1

我有一个查询:

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

默认情况下使用内部连接。左连接给了我左表的所有结果。与右连接相同。

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

我想要这样的结果

AMD, NULL
ACER, NULL
NULL, INTEL
NULL, ATI

有什么办法吗?

4

2 回答 2

1

您需要一个 完整的外连接,不幸的是,它没有在中实现MYSQL,您可以使用unionofleftrightjoin :

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`
于 2012-11-10T08:36:02.803 回答
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小提琴

于 2012-11-10T08:48:39.323 回答