用 exists 和 not exists求集合的交集(intersection)和差集(except)及除法
来源:互联网 发布:linux搜狗输入法异常 编辑:程序博客网 时间:2024/06/17 17:09
用 exists 和 not exists求集合的交集(intersection)和差集(except)及除法
(本文中所有SQL语句均在Mysql v5.1中验证)
求交集intersection和差集except可以用 (A in B 或 A exists B 或 =some)和 (A not in B 或 A not exists B 或 <>all)处理:如下
mysql> select * from a;
+------+------+---------------------+
| id | name | Date |
+------+------+---------------------+
| 1 | A1 | 2011-07-01 00:00:00 |
| 2 | A2 | NULL |
| 3 | A3 | NULL |
+------+------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from b;
+------+------+------+
| id | aid | name |
+------+------+------+
| 1 | 1 | B1 |
| 2 | 2 | B2 |
| 3 | 2 | B3 |
+------+------+------+
3 rows in set (0.00 sec)
用exists求表a的id列和表b的aid列的交集(a.id ∩ b.aid)。
mysql> select id from a where exists (select * from b where a.id=b.aid);
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
用exists求表a的id列和表b的aid列的差集(a.id - b.aid):
mysql> select id from a where not exists (select * from b where a.id=b.aid);
+------+
| id |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
求除法请参考另外一份文档:exists和not exists的嵌套使用及求关系的除法.txt
(本文中所有SQL语句均在Mysql v5.1中验证)
求交集intersection和差集except可以用 (A in B 或 A exists B 或 =some)和 (A not in B 或 A not exists B 或 <>all)处理:如下
mysql> select * from a;
+------+------+---------------------+
| id | name | Date |
+------+------+---------------------+
| 1 | A1 | 2011-07-01 00:00:00 |
| 2 | A2 | NULL |
| 3 | A3 | NULL |
+------+------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from b;
+------+------+------+
| id | aid | name |
+------+------+------+
| 1 | 1 | B1 |
| 2 | 2 | B2 |
| 3 | 2 | B3 |
+------+------+------+
3 rows in set (0.00 sec)
用exists求表a的id列和表b的aid列的交集(a.id ∩ b.aid)。
mysql> select id from a where exists (select * from b where a.id=b.aid);
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
用exists求表a的id列和表b的aid列的差集(a.id - b.aid):
mysql> select id from a where not exists (select * from b where a.id=b.aid);
+------+
| id |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
求除法请参考另外一份文档:exists和not exists的嵌套使用及求关系的除法.txt
- 用 exists 和 not exists求集合的交集(intersection)和差集(except)及除法
- exists和not exists的嵌套使用及求关系的除法
- EXISTS 和 NOT EXISTS
- EXISTS 和 NOT EXISTS
- exists和not exists
- not exists和exists
- 求两个集合的交集、差集和并集
- exists 和 not exists的详细解释
- exists/not exists的用法和原理
- sql 的EXISTS和NOT EXISTS
- exists和not exists的使用
- oracle exists和not exists的使用
- SQL - exists和not exists
- Exists 和 Not Exists 备忘
- mysql EXISTS和NOT EXISTS
- 用顺序表求集合的交集、并集和差集
- exists 和not exists 用法简介及与IN和not In的区别
- exists 和not exists 用法简介及与IN和not In的区别
- Oracle安装错误ora-00922
- MySQL中 in, some, all等用法的讨论
- MySQL中各种join
- SQL - group by
- 歪理邪说解析架构设计师上午考试试题之四(分析2010下半年系统架构设计师上午试题16-20题)
- 用 exists 和 not exists求集合的交集(intersection)和差集(except)及除法
- AWK学习
- SQL 2008重装之路
- 【cocos2d-x 初级教程之二 2.x版本】如何添加一个cocos2d-x精灵
- 蒋佩蓉:中国崛起,我们拿什么来领跑?
- < Photoshop CS 专栏 >基础应用(一)
- 麦咖啡与360安全卫士及优化大师均有冲突
- 歪理邪说解析架构设计师上午考试试题之五(分析2010下半年系统架构设计师上午试题21-25题)
- jquery统计文本域输入字数实现