我如何外连接两个mysql查询?

时间:2012-07-23 06:22:08

标签: mysql

假设有4个表,就是这样 t1.id = t2.pidt3.id = t4.pid

如何获取t1.val1 != t3.val1 AND t2.val2 != t4.val2

的记录

1 个答案:

答案 0 :(得分:0)

您需要使用LEFT OUTER JOIN从两个表中查找不匹配的记录。

SELECT *
FROM t1
     INNER JOIN t2
           ON t1.id = t2.pid
     LEFT OUTER JOIN t3
           ON t2.pid = t3.id
     LEFT OUTER JOIN t4
           ON t3.id = t4.pid
WHERE (t1.val1 IS NULL OR t3.val1 IS NULL)
      AND (t2.val2 IS NULL OR t4.val2 IS NULL)

访问:http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html