记录工作空闲之余学习 遇到的问题的汇总@Mysql
题目:
题目:现在运营想要分别查看学校为山东大学或者性别为男性的用户的device_id、gender、age和gpa数据,请取出相应结果,结果不去重。
我的答案
select device_id,gender,age,gpa from user_profile where university = “山东大学” or gender = “male”
实际答案
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’
学习内容
只要满足一个条件就被筛选出来,但总会存在一个人满足了两个条件只筛选一次。这里的坑时使用or,因为or自带去重,而union等价于or,但union all 可以不去重,所以本体考察or与union的细节使用。
知识点学习
union :合并结果集,不重,默认规则的排序
unionAll :对结果集进行并集操作,不去重,不排序
or:满足两个条件的并集,去重,不排序
union、unionAll
合并结果集,需要去重就用UNION,不需要去重就用UNION ALL,如果两个结果集中没有重复的结果集,就用UNION ALL