描述
题目:现在运营想要分别查看学校为山东大学或者性别为男性的用户的device_id、gender、age和gpa数据,请取出相应结果,结果不去重。
注意
其实这里的不去重表示:只要满足一个条件就被筛选出来,但总会存在一个人满足了两个条件只筛选一次。这里的坑时使用or,因为or自带去重,而union等价于or,但union all 可以不去重,所以本体考察or与union的细节使用。
示例: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 |
根据示例,你的查询应返回以下结果(注意输出的顺序,先输出学校为山东大学再输出性别为男生的信息):
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 |
题解
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'