union对两个结果集进行并集操作,不包括重复行,相当于distinct,同时进行默认规则的排序;
union all:对两个结果集进行并集操作,包括重复行,即所有的结果全部显示,不管是不是重复。
示例:user_profile
id | device_id | gender | age | university | gpa | active_days_within_30 | question_cnt | answer_cnt |
1 | 2138 | male | 21 | 北京大学 | 3.4 | 7 | 2 | 12 |
2 | 3214 | male | 复旦大学 | 4 | 15 | 5 | 25 | |
3 | 6543 | female | 20 | 北京大学 | 3.2 | 12 | 3 | 30 |
4 | 2315 | female | 23 | 浙江大学 | 3.6 | 5 | 1 | 2 |
5 | 5432 | male | 25 | 山东大学 | 3.8 | 20 | 15 | 70 |
6 | 2131 | male | 28 | 山东大学 | 3.3 | 15 | 7 | 13 |
7 | 4321 | male | 26 | 复旦大学 | 3.6 | 9 | 6 | 52 |
select device_id, gender, age, gpa
from user_profile
where university ='山东大学'
union all --结果不去重就用union all,去重就用 union。
select device_id, gender, age, gpa
from user_profile
where gender = 'male'
根据示例,你的查询应返回以下结果(注意输出的顺序,先输出学校为山东大学再输出性别为男生的信息):
device_id | gender | age | gpa |
5432 | male | 25 | 3.8 |
2131 | male | 28 | 3.3 |
2138 | male | 21 | 3.4 |
3214 | male | None | 4 |
5432 | male | 25 | 3.8 |
2131 | male | 28 | 3.3 |
4321 | male | 28 | 3.6 |
本题注意:age为null 也记为 25岁以下
方法1:
select '25岁以下' age_cut,count(device_id) as number
from user_profile
where age < 25 or age IS NULL
union all
select '25岁及以上' age_cut,count(device_id) as number
from user_profile
where age >= 25 方法2:
select age_cut, count(*)
from (selectif(age>=25, "25岁及以上", "25岁以下") as age_cutfrom user_profile
) as up
group by age_cut方法3:
SELECT case when age<25 OR age IS null then "25岁以下"else "25岁及以上"end as age_cut,count(device_id) as number
FROM user_profile
GROUP BY age_cut
返回以下结果:
age_cut | number |
25岁以下 | 4 |
25岁及以上 | 3 |
方法1:
select device_id,gender,if(age is null, '其他',if(age<20,'20岁以下',if(age<=24,'20-24岁','25岁及以上'))) age_cut
from user_profile方法2:
select device_id,gender,case when age<20 then '20岁以下'when 20<=age and age<25 then '20-24岁'when age>=25 then '25岁及以上'else '其他'end age_cut
from user_profile
返回以下结果:
device_id | gender | age_cut |
2138 | male | 20-24岁 |
3214 | male | 其他 |
6543 | female | 20-24岁 |
2315 | female | 20-24岁 |
5432 | male | 25岁及以上 |
2131 | male | 25岁及以上 |
4321 | male | 25岁及以上 |