您的位置:首页 > 数据库 > MySQL

MySQL全连接(Full Join)实现

2013-07-07 20:21 435 查看
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>

参考:http://www.artfulsoftware.com/infotree/queries.php
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MySQL