所以我必须进行查询以返回所有不包含“apple”项目的收据号。
数据如下。 EG 如果您去商店购买苹果和香蕉,数据将是:
(table reciepts)
recieptNumber productCode
12345 9999
12345 8888
(table products)
productCode productName
9999 Apples
8888 Bananas
我在想:
SELECT reciepts.recieptNumber
FROM reciepts JOIN products
ON reciepts.productCode == products.productCode
WHERE products.productName == "Apples"
GROUP BY reciepts.recieptNumber
HAVING COUNT(*) == 0;
但我现在知道,在 count = 0 时,having 不起作用,因为没有什么可计算的。
有什么建议吗?
最佳答案
你的方法是在正确的轨道上:-)
SELECT reciepts.recieptNumber
FROM reciepts
-- switch to Outer Join
LEFT JOIN products
ON reciepts.productCode = products.productCode
-- this will result in a NULL row when there's no Apple in the receipt
-- otherwise a row with 'Apples'
AND products.productName = 'Apples'
GROUP BY reciepts.recieptNumber
-- if there's only a NULL you found the matching receipt
HAVING COUNT(products.productCode) = 0;
关于mysql - SQL - 替换 HAVING COUNT(*) == 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63842965/