题意:

分别查看学校为山东大学或者性别为男性的用户的device_id、gender、age和gpa数据


分解:

  • 由学校为山东大学或性别为男性,知:university='山东大学', gender='male'
  • 由分别查看,知不去重,即:union all

代码:

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'

#where university='山东大学' or gender='male'  结果会去重

补充知识点:

  • union all 用法
select a,b,c from 表1  where 条件A
union all 
select a,b,c from 表1  where 条件B
  • 区别: union 会去重,union all 不去重 all表示在最后结果中不去重展示所有
  • 如果有分组排序,ORDER BY语句要放在最后一个查询语句的后边