MySQL Inner join with Not equal operator

13,595

Solution 1

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

Solution 2

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

Solution 3

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;
Share:
13,595
Tigran Tokmajyan
Author by

Tigran Tokmajyan

I'm a software engineer. Mostly doing web-development using PHP/Node.JS, but also love doing game development with Unity3D.

Updated on June 11, 2022

Comments

  • Tigran Tokmajyan
    Tigran Tokmajyan almost 2 years

    I 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.