0

我喜欢获取尚未包含在 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 ) 

请问有什么帮助吗?

4

4 回答 4

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)

您现在正在做的,是加入与from 不匹配的customer_machinefor each from ;所以你正在增加你的结果。serial_noserial_numbercustomer_machine

于 2013-03-13T18:38:16.667 回答
1

如果你加入Productserial_number表格,结果将是serial numbers which are in products but not in customer_machine. 由于上述答案处理了这种情况,因此此查询将使用 a和忽略表进行选择serial numbers which are not in customer_machine(如您所述) 。left joinProduct

Sql-Server 小提琴演示

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
于 2013-03-13T18:46:05.590 回答
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
于 2013-03-13T18:40:19.920 回答
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 )
于 2013-03-13T18:41:07.437 回答