MySQL比较2个表值

时间:2012-11-10 08:26:20

标签: mysql join

我得到了一个问题:

SELECT  a.Vendor, i.Vendor
FROM    whole1 a 
        LEFT JOIN whole2 i 
        ON (a.Vendor = i.Vendor) 
GROUP BY a.Vendor, i.Vedor 
LIMIT 0, 200

默认使用Inner JOIN。左连接给我左表的所有结果。与右连接相同。

我正在寻找的是左右连接的组合,但不是内部加入。

我希望得到像

这样的结果
AMD, NULL
ACER, NULL
NULL, INTEL
NULL, ATI

有什么办法吗?

2 个答案:

答案 0 :(得分:1)

你需要full outer join, 遗憾的是,MYSQL未对其进行实施,您可以使用unionleft加入right

SELECT a.`Vendor`, i.`Vendor`
FROM `whole1` a LEFT JOIN whole2 i ON (a.`Vendor` = i.`Vendor`) 
GROUP BY a.`Vendor`, i.`Vendor` 

UNION

SELECT a.`Vendor`, i.`Vendor`
FROM `whole1` a RIGHT JOIN whole2 i ON (a.`Vendor` = i.`Vendor`) 
GROUP BY a.`Vendor`, i.`Vendor`

答案 1 :(得分:1)

使用带有左和右连接的联合以及带有限制的选择

SELECT * FROM (
  SELECT 
    a.`Vendor` v1, i.`Vendor` v2
  FROM 
    `whole1` a 
  LEFT JOIN 
    whole2 i ON (a.`Vendor` = i.`Vendor`) 
  GROUP BY 
    a.`Vendor`, i.`Vendor` 

  UNION

  SELECT 
    a.`Vendor`, i.`Vendor`
  FROM 
    `whole1` a 
  RIGHT JOIN 
    whole2 i ON (a.`Vendor` = i.`Vendor`) 
  GROUP BY 
    a.`Vendor`, i.`Vendor`
) AS whole
LIMIT 0,200

SQL Fiddle