如何在两个表中提取非重复值(How to extract non-duplicate values in two tables)
我有两个表,每个表包含带数字的列。 我需要比较两个表中的列并提取第一个表中存在的数字,而不是第二个表中存在的数字。 我不需要独特的价值。
我写了这个查询:
SELECT Table1.Numbers, Table1.Name FROM Table1, Table2 WHERE Table1.Numbers != Table2.numbers由于我正在处理数百万条记录,有人可以推荐更有效的查询,这会给我提供相同的结果吗?
I have two tables where each one contains columns with numbers. I need to compare columns in both tables and extract the number that does exist in first table, and does not exist in second one. I don't need unique value.
I wrote this query:
SELECT Table1.Numbers, Table1.Name FROM Table1, Table2 WHERE Table1.Numbers != Table2.numbersSince I am working on several million records can someone recommend more efficient query which would provide me with identical results?
最满意答案
我会使用NOT EXISTS :
SELECT Table1.Numbers, Table1.Name FROM Table1 WHERE NOT EXISTS( SELECT 1 FROM Table2 WHERE Table1.Numbers=Table2.Numbers )其他方法:
我应该使用NOT IN,OUTER APPLY,LEFT OUTER JOIN,EXCEPT还是NOT EXISTS?
I would use NOT EXISTS:
SELECT Table1.Numbers, Table1.Name FROM Table1 WHERE NOT EXISTS( SELECT 1 FROM Table2 WHERE Table1.Numbers=Table2.Numbers )Other approaches:
Should I use NOT IN, OUTER APPLY, LEFT OUTER JOIN, EXCEPT, or NOT EXISTS?
更多推荐
发布评论