2011-08-26 60 views
4

我在Sale中有很多行,並且SalesProcessed中有一行。MySQL內部連接不等於運算符

SELECT * FROM Sale 
    INNER JOIN SalesProcessed 
    ON Sale.id<>SalesProcessed.id 
    LIMIT 0,30 

此代碼返回SalesProcessed中id相同的行。爲什麼?

其實我需要銷售行中哪些ID在SalesProcessed中不存在。

回答

10
SELECT * 
    FROM Sale 
     LEFT JOIN SalesProcessed 
      ON Sale.id = SalesProcessed.id 
    WHERE SalesProcessed.id IS NULL 
    LIMIT 0,30 
4

另一種方法

SELECT * FROM Sale 
where Sale.id not in (select SalesProcessed.id from SalesProcessed) 
LIMIT 0,30 



SELECT * FROM Sale 
where NOT EXISTS (
select SalesProcessed.id from SalesProcessed where Sale.id=SalesProcessed.id) 
LIMIT 0,30 

您應該檢查與得到最好的結果

4

如果你想不存在行解釋每個查詢,這是錯誤的查詢:

SELECT * 
FROM Sale 
LEFT JOIN SalesProcessed 
ON Sale.ID = SalesProcessed.id 
WHERE SalesProcessed.id IS NULL; 
+0

左連接是我錯過的東西。現在明白了。謝謝 – pown