[英]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. 如何从表中选择数据不存在于另一个表sql中。 I've tried NOT IN and NOT EXIST methods. 我尝试过NOT IN和NOT EXIST方法。 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: 我认为你的table table1
和table2
在其name
列上有索引,所以你可以试试这个:
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
可能是id列存在,如果不存在,请使用t2.name
替换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)
. 你想要table2(name)
上的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.