mysql实现full outer join

由于MySQL设计时不支持full outer join,

所以如果有全连接需求时,需要一点小技巧来实现。

假设有两个表t1,t2
full outer join 的效果和下面的效果一样:

SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id

如果有多于2个表 t1,t2,t3
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 ON t2.id = t3.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 ON t2.id = t3.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
RIGHT JOIN t3 ON t2.id = t3.id

posted @ 2017-08-02 09:03  xushukui  阅读(520)  评论(0编辑  收藏  举报