MySQL全连接(Full Join)实现

MySQL本身不支持你所说的full join(全连接),但可以通过union来实现
下面是一个简单测试,可以看看:

mysql> CREATE TABLE a(id int,name char(1)); 
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE b(id int,name char(1)); 
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO a VALUES(1,‘a‘),(2,‘b‘); 
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> INSERT INTO b VALUES(2,‘b‘),(3,‘c‘);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM a LEFT JOIN b ON a.id=b.id 
-> UNION 
-> SELECT * FROM a RIGHT JOIN b ON a.id=b.id;
+------+------+------+------+
| id | name | id | name |
+------+------+------+------+
| 1 | a | NULL | NULL |
| 2 | b | 2 | b |
| NULL | NULL | 3 | c |
+------+------+------+------+
3 rows in set (0.00 sec)
mysql>
 

MySQL全连接(Full Join)实现,布布扣,bubuko.com

MySQL全连接(Full Join)实现

上一篇:mysql通过表和function模拟oracle的sequence


下一篇:在论坛中出现的比较难的sql问题:26(动态行专列+合并字符串)