简体   繁体   中英

How to select data from the table not exist in another table sql

How to select data from the table not exist in another table sql. I've tried NOT IN and NOT EXIST methods. But it causes performance issues for large amount of data. Can anyone suggest a solution for this.? Thanks in advance.

I've tried the following.

SELECT name 
FROM table1
WHERE NOT EXISTS 
    (SELECT * 
     FROM table2 
     WHERE table1.name = table2.name)

And NOT IN Cases. But performance issues while a for large number of data.

I think your table table1 and table2 have index on their name column, so you can try this:

SELECT name
FROM table1 t1 LEFT JOIN table2 t2 ON t1.name = t2.name
WHERE t2.id IS NULL

May be id column existed, if not, use t2.name as a replacement for t2.id

For this query:

SELECT name 
FROM table1
WHERE NOT EXISTS 
    (SELECT * 
     FROM table2 
     WHERE table1.name = table2.name)

You want an index on table2(name) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM