0

所以我试图找出一种方法,将一列的每一行与另一列中的所有行进行比较,并过滤掉与之比较的列中不存在的那些。我了解如何逐行比较两列,如下所示:

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 
4

4 回答 4

1

尝试不输入:

select table1.column1
from table1
where table1.column1 not in (select table2.column2 from table2)
于 2012-08-17T13:32:54.907 回答
0
select column1 from table1 as t1 where not exists
(select column2 from table2 as t2 where t1.id=t2.id)
于 2012-08-17T13:32:34.967 回答
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)
于 2012-08-17T13:33:42.517 回答
0
select t1.id
from ... t1
left join ... t2 on t1.id = t2.id
where t2.id is null
于 2012-08-17T13:44:33.790 回答