MySQL 的並集、交集和差集 交集和差集類似oracle中minus的用法

MySQL 的並集、交集和差集

table1 如下:
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  4 | c    |
|  5 | d    |
|  6 | e    |
+----+------+

table2 如下:
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
|  5 | d    |
+----+------+

兩個表的並集使用如下語句:
select * from table1 union select * from table2
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  4 | c    |
|  5 | d    |
|  6 | e    |
|  2 | b    |
|  3 | c    |
+----+------+

兩個表的交集使用如下語句:
select table1.id,table2.name from table1 left join table2 using (id) where table2.id is not null
+----+------+
| id | name |
+----+------+
|  4 | c    |
|  6 | e    |
+----+------+

表1和表2的交集使用如下語句:
select table1.id,table2.name from table1 left join table2 using (id) where table2.id is null
+----+------+
| id | name |
+----+------+
|  4 | c    |
|  6 | e    |
+----+------+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章