描述

  • 法1 union
SELECT device_id, gender, age, university,gpa from user_profile where gpa > 3.8 and university = '复旦大学' UNION
SELECT device_id, gender, age, university,gpa from user_profile where gpa > 3.5 and university = '山东大学'
  • 法2 子查询
select device_id,gender,age,university,gpa
from user_profile
where
device_id in (select device_id from user_profile where gpa>3.5 and university='山东大学')
or
device_id in (select device_id from user_profile where gpa>3.8 and university='复旦大学')
  • 法3 or
#语句1
SELECT device_id, gender, age, university,gpa 
from user_profile 
where (gpa > 3.8 and university = '复旦大学') or (gpa > 3.5 and university = '山东大学')
#语句2
SELECT device_id,gender,age,university,gpa
FROM user_profile
WHERE gpa > 3.5 and university = '山东大学' OR gpa > 3.8 and university = '复旦大学';

值得一提的是,不仅仅是在Sql Server中,电路中、编程语言中都是and的优先级高于or,即:逻辑与的优先级比逻辑或要高,但是同时低于逻辑非。
下次研究一下从执行速率、内存消耗上分析,加括号和不加括号有什么区别

posted on 2022-03-02 17:33  秉烛爱好者  阅读(134)  评论(0编辑  收藏  举报