所以我试图找出一种方法来比较列的每一行与另一列中的所有行,并过滤掉它所比较的列中不存在的那些行。我理解如何比较逐行基本的两列如下:
select table1.column1
from table1 inner join
table2 on table1.id = table2.id
where column1 <> column2
但我想比较column1中table1的行与table2中column2的所有行,并查找column1中根本不存在于column2中的行。所以它就像具有这些值的列:
Column1 Column2
1 2
2 1
4 3
5 5
7 6
在SQL之后它会变成这样:
Column1
4
7
答案 0 :(得分:1)
尝试使用NOT IN:
select table1.column1
from table1
where table1.column1 not in (select table2.column2 from table2)
答案 1 :(得分:0)
select column1 from table1 as t1 where not exists
(select column2 from table2 as t2 where t1.id=t2.id)
答案 2 :(得分:0)
如果比较2个表之间的列
SELECT table1.column1 FROM table1
WHERE table1.column1 NOT IN
(SELECT table2.column2 as column1 FROM table2)
如果将column1与column2与同一个表
进行比较SELECT table1.column1 FROM table1
WHERE table1.column1 NOT IN
(SELECT table1.column2 as column1 FROM table1)
答案 3 :(得分:0)
select t1.id
from ... t1
left join ... t2 on t1.id = t2.id
where t2.id is null