SQL25 查找山东大学或者性别为男生的信息
描述
题目:现在运营想要分别查看学校为山东大学或者性别为男性的用户的device_id、gender、age和gpa数据,请取出相应结果,结果不去重。
结果示例:
错误代码:
利用where or会把山东大学且男性的数据过滤掉
select
device_id,
gender,
age,
gpa
from
user_profile
where
university='山东大学'
or gender='male'
order by university,gender
正确代码:
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
1.union 和union all的区别
union是合并两个查询语句的结果集,并排除重复项
union all是不排除重复项的
2.union使用前提
使用union合并两个表时,需要两个表的结果集字段完全一样。
表一(SELECT device id,gender,age,gpa )
表二(SELECT device id,gender,age,gpa)