开发者

MySQL Inner join with Not equal operator

开发者 https://www.devze.com 2023-03-31 06:38 出处:网络
I 开发者_Go百科have many rows in Sale, and one Row in SalesProcessed. SELECT * FROM Sale INNER JOIN SalesProcessed

I 开发者_Go百科have many rows in Sale, and one Row in SalesProcessed.

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

This code returns same row which id is in SalesProcessed. Why?

Actually I need Sale rows which ID's doesn't exist in SalesProcessed.


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


Another approach

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

You should check each query with explain for getting the best result


If you want non-existing rows, that's the wrong query:

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

精彩评论

暂无评论...
验证码 换一张
取 消