简体   繁体   English

如何从表中选择数据不存在于另一个表sql中

[英]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 table1table2在其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.

相关问题 sql select 值来自另一个表(如果存在) - sql select values from another table if exist 如何从表中进行选择并检查是否存在于另一个表上? - How to select from table with check if exist on another table? 如何从SQL Server中的另一个数据库中选择表的数据? - How to select data of a table from another database in SQL Server? SQL仅在数据存在时才从其他表中选择colmn - SQL Select colmn from other table only if data exist 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? 从一个表中选择数据,然后插入到该表中不存在的另一个现有表中 - Select data from one table and insert into another existing table, which doesn't exist in the table SQL:如何从一个表中选择最大值,而从另一表中选择其他数据 - SQL: how to select max value from one table and other data from another table SQL-检查数据是否也存在于另一个表上 - SQL - Check if data EXIST on another table too 如果在另一个表中不存在数据,如何从表中进行选择 - How to SELECT from a table if no data exists in another SQL:从表中选择仅在具有特定ID的另一个表中不存在的行的最佳方法是什么? - SQL: What is the best way to select rows from a table that do not exist in another table only with a particular id?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM