LEFT OUTER JOIN 和 INNER JOIN 使总行数相等

LEFT OUTER JOIN with INNER JOIN to Equal Total Rows

我有生产者和产品table。我正在尝试编写 2 个查询,有产品的生产者和没有产品的生产者。

从每个 Query 返回的行加起来应该等于 Producer 中的总行 Table,但我的代码不是这种情况。两个查询的总和为 819,但生产者 table 中只有 766 行。重复项来自哪里?

我的 JOIN 做错了什么?

PRODUCER TABLE
+------+--------+------+------+
| producerID  | producerName  |
+------+--------+------+------+
|  123        | Toys R Us     |
|  234        | GameStop      |
|  345        | Amazon        |
+------+--------+------+------+

PRODUCT TABLE
+------+--------+------+--------+------+
| productID | productName | producerID |
+------+--------+------+--------+------+
|  1        | Mega Man    |  123       |
|  2        | Lemmings    |  234       |
|  3        | Mario Kart  |  234       |
+------+--------+------+--------+------+


/*STORES CARRYING Products*/
/*This query returns 169*/
SELECT producerName, pt.producerID, productName
FROM Product pt 
INNER JOIN Producer pd ON pt.producerID = pd.producerID 

/*STORES NOT CARRYING Products*/
/*This query returns 650 */
SELECT producerName, pt.producerID, productName
FROM Producer pd
LEFT OUTER JOIN Product pt ON pt.producerID = pd.producerID
WHERE pt.producerID IS NULL

/*Count all rows returns 766*/
SELECT COUNT(*) FROM Producer

您可以使用 Subquery.

而不是使用联接

例如,此查询将 return 所有拥有产品的生产商。

SELECT *
FROM Producer
WHERE producerID IN (SELECT producerID FROM Products)

并且此查询将 return 所有没有产品的生产商。

SELECT *
FROM Producer
WHERE producerID NOT IN (SELECT producerID FROM Products)