我想获取尚未包含在Customer_Machine表中的序列号。但相反,行是交叉乘法。这是我的代码。
SELECT serial_number.serial_no
FROM product
INNER JOIN serial_number
ON product.productid = serial_number.productid
INNER JOIN customer_machine
ON NOT( customer_machine.serial_no = serial_number.serial_no )
有什么帮助吗?
答案 0 :(得分:1)
这应该做:
SELECT serial_number.serial_no
FROM product P
INNER JOIN serial_number S
ON P.productid = S.productid
WHERE NOT EXISTS (SELECT 1 FROM customer_machine
WHERE serial_no = S.serial_no)
您现在正在做的是加入customer_machine
来自serial_no
的{{1}}来自serial_number
的与customer_machine
相同的数字;所以你的结果会倍增。
答案 1 :(得分:1)
如果您加入Product
和serial_number
表,结果将为serial numbers which are in products but not in customer_machine
。由于上述答案处理了这种情况,因此该查询将使用serial numbers which are not in customer_machine
并忽略left join
表来选择Product
(如您所述)。
select s.serial_no
from serial_number s
left join customer_machine c on s.serial_no = c.serial_no
where c.serial_no is null
答案 2 :(得分:0)
SELECT serial_number.serial_no
FROM product
INNER JOIN serial_number
ON product.productid = serial_number.productid
LEFT JOIN customer_machine
ON ( customer_machine.serial_no = serial_number.serial_no)
WHERE customer_machine.serial_no IS NULL
答案 3 :(得分:0)
您可能只想排除已存在的特定序列号,而不是将它们加入。您可以在where子句中执行此操作:
SELECT serial_number.serial_no
FROM product
INNER JOIN serial_number
ON product.productid = serial_number.productid
WHERE serial_number.serial_no NOT IN ( SELECT customer_machine.serial_no
FROM customer_machine )