赞
踩
SELECT id,device_id,gender,age,university,province from user_profile
不建议select * from
select distinct university from user_profile 使用distinct
select university from user_profile group by university; 使用group by
SELECT
*
FROM
table
LIMIT 5,5,使用limit
SELECT device_id FROM user_profile ORDER BY id LIMIT 2
SELECT device_id AS user_infos_example FROM user_profile LIMIT 2 使用as
select device_id,age from user_profile order by age asc; // order by + 列名 asc/desc:根据那一列升序/降序 默认升序
select device_id,gpa,age from user_profile order by gpa asc,age asc; order by 可以多个
select device_id,gender,age,university from user_profile where age is not NULL;
SELECT device_id,gender,age,university,gpa FROM user_profile WHERE university IN ('北京大学','复旦大学','山东大学')
select device_id,gender,age,university,gpa from user_profile where (university='山东大学' and gpa>3.5 ) or (university="复旦大学" and gpa>3.8);
SELECT device_id,age,university FROM user_profile WHERE university LIKE '%北京%'
# 方法1
# select max(gpa) as gpa
# from user_profile
# where university='复旦大学';
#方法2
select gpa
from user_profile
where university='复旦大学'
order by gpa desc limit 1
SELECT COUNT(gender)male_num,ROUND(AVG(gpa),1) AS avg_gpa FROM user_profile
WHERE gender='male' count统计数量,保留一位小数用round
SELECT university,AVG(question_cnt)avg_quesition_cnt,AVG(answer_cnt)avg_answer_cnt
FROM user_profile
GROUP BY university
HAVING avg_quesition_cnt < 5 OR avg_answer_cnt < 20
先分组再筛选 group by having
-- join 以及子查询 方法1: select qpd.device_id, qpd.question_id, qpd.result from question_practice_detail as qpd inner join user_profile as up on up.device_id=qpd.device_id and up.university='浙江大学'
方法2:
select device_id, question_id, result from question_practice_detail where device_id in ( select device_id from user_profile where university='浙江大学' )
方法3: select a.device_id device_id,a.question_id question_id,a.result result from question_practice_detail a,user_profile b where b.device_id = a.device_id and university = '浙江大学'
select university,
count(question_id) / count(distinct qpd.device_id) as avg_answer_cnt
from question_practice_detail as qpd
inner join user_profile as up
on qpd.device_id=up.device_id
group by university
SELECT
u.university,
qd.difficult_level,
count(q.question_id)/count(distinct(q.device_id)) AS avg_answer_cnt
FROM question_practice_detail AS q
LEFT JOIN user_profile AS u
ON u.device_id=q.device_id
LEFT JOIN question_detail AS qd
ON q.question_id=qd.question_id
GROUP BY u.university, qd.difficult_level;
SELECT device_id,gender,age,gpa FROM user_profile
WHERE university='山东大学'
UNION ALL
SELECT device_id,gender,age,gpa FROM user_profile
WHERE gender="male"
结果不去重就用union all,去重就用 union。
where university='山东大学' or gender="male"的话,也是去重的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。