DB 的 and 与 or 顺序和括号细究
创建测试数据:
drop table IF EXISTS t_and_or;
create table t_and_or(id int,ttype varchar(3),tdate date,mbid varchar(6));
insert into t_and_or values(1,'SYD','2024-05-20','M101'),(2,'SYD','2024-05-20','M102'),(3,'SYD','2024-05-20','M103'),(4,'SYD','2024-06-20','M101'),(5,'SYD','2024-06-20','M102');
select * from t_and_or;
复制
拿PostgreSQL数据库来看看:
-- PG
postgres=# select * from t_and_or;
id | ttype | tdate | mbid
----+-------+------------+------
1 | SYD | 2024-05-20 | M101
2 | SYD | 2024-05-20 | M102
3 | SYD | 2024-05-20 | M103
4 | SYD | 2024-06-20 | M101
5 | SYD | 2024-06-20 | M102
(5 rows)
-- and 与 or 的顺序和括号问题
select * from t_and_or where tdate = '2024-05-20' and (ttype = 'SYD' or mbid in ('M101','M102'));
select * from t_and_or where (tdate = '2024-05-20' and ttype = 'SYD') or mbid in ('M101','M102');
select * from t_and_or where tdate = '2024-05-20' and ttype = 'SYD' or mbid in ('M101','M102');
select * from t_and_or where tdate = '2024-05-20' and (ttype = 'SYD' and (mbid = 'M101' or mbid = 'M102'));
select * from t_and_or where tdate = '2024-05-20' and (ttype = 'SYD' and mbid = 'M101' or mbid = 'M102');
复制
按括号进行拆分后逐块分析,and 即交集,or 即并集
结果 = 红框 N 绿框 = 红框 N(黄框 U 蓝框)
结果 = 红框 和 绿框 的交集 =(黄框 和 蓝框 的并集)再与 红框 的交集
postgres=# select * from t_and_or where tdate = '2024-05-20' and (ttype = 'SYD' or mbid in ('M101','M102'));
id | ttype | tdate | mbid
----+-------+------------+------
1 | SYD | 2024-05-20 | M101
2 | SYD | 2024-05-20 | M102
3 | SYD | 2024-05-20 | M103
(3 rows)
复制
结果 = 绿框 U 黄框 = (红框 N 蓝框)U 黄框
结果 = 绿框 和 黄框 的并集 = (红框 和蓝框 的交集)再和 黄框 的并集
postgres=# select * from t_and_or where (tdate = '2024-05-20' and ttype = 'SYD') or mbid in ('M101','M102');
id | ttype | tdate | mbid
----+-------+------------+------
1 | SYD | 2024-05-20 | M101
2 | SYD | 2024-05-20 | M102
3 | SYD | 2024-05-20 | M103
4 | SYD | 2024-06-20 | M101
5 | SYD | 2024-06-20 | M102
(5 rows)
postgres=# select * from t_and_or where tdate = '2024-05-20' and ttype = 'SYD' or mbid in ('M101','M102');
id | ttype | tdate | mbid
----+-------+------------+------
1 | SYD | 2024-05-20 | M101
2 | SYD | 2024-05-20 | M102
3 | SYD | 2024-05-20 | M103
4 | SYD | 2024-06-20 | M101
5 | SYD | 2024-06-20 | M102
(5 rows)
复制
结果 = 红框 N 蓝框 = 红框 N (黄框 N 绿框)
结果 = 红框 和 蓝框的交集 =(黄框 和 绿框 的交集)再和 红框 的交集
postgres=# select * from t_and_or where tdate = '2024-05-20' and (ttype = 'SYD' and (mbid = 'M101' or mbid = 'M102'));
id | ttype | tdate | mbid
----+-------+------------+------
1 | SYD | 2024-05-20 | M101
2 | SYD | 2024-05-20 | M102
(2 rows)
postgres=# select * from t_and_or where tdate = '2024-05-20' and (ttype = 'SYD' and mbid = 'M101' or mbid = 'M102');
id | ttype | tdate | mbid
----+-------+------------+------
1 | SYD | 2024-05-20 | M101
2 | SYD | 2024-05-20 | M102
(2 rows)
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
文章被以下合辑收录
评论
数据库 的 and 与 or 眼花缭乱的顺序和括号
3月前

评论
相关阅读
数据库国产化替代深化:DBA的机遇与挑战
代晓磊
1329次阅读
2025-04-27 16:53:22
2025年4月国产数据库中标情况一览:4个千万元级项目,GaussDB与OceanBase大放异彩!
通讯员
806次阅读
2025-04-30 15:24:06
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
542次阅读
2025-04-17 17:02:24
一页概览:Oracle GoldenGate
甲骨文云技术
506次阅读
2025-04-30 12:17:56
GoldenDB数据库v7.2焕新发布,助力全行业数据库平滑替代
GoldenDB分布式数据库
484次阅读
2025-04-30 12:17:50
优炫数据库成功入围新疆维吾尔自治区行政事业单位数据库2025年框架协议采购!
优炫软件
370次阅读
2025-04-18 10:01:22
给准备学习国产数据库的朋友几点建议
白鳝的洞穴
357次阅读
2025-05-07 10:06:14
XCOPS广州站:从开源自研之争到AI驱动的下一代数据库架构探索
韩锋频道
320次阅读
2025-04-29 10:35:54
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
数据库运维之道
306次阅读
2025-04-28 11:01:25
国产数据库图谱又上新|82篇精选内容全览达梦数据库
墨天轮编辑部
290次阅读
2025-04-23 12:04:21