我需要做的是以下几点:
我的数据库中有一个这样的表:
idx | name | age
------ ---------- -------
1 | John | 18
2 | Marry | 19
3 | Eric | 17
然后我得到一个secondTable:
name | age
------ -----
Moses | 29
John | 18
Eric | 20
我想运行一个 except 查询,例如:
select *
from firstTable
where (name, age) not in (select * from secondTable)
和这样的相交查询:
select *
from firstTable
where (name, age) in (select * from secondTable)
所以第一个查询的结果将是:
2 | Marry | 19
---- -------- ----
3 | Eric | 17
第二个查询的结果将是:
1 | John | 18
我还找到了以下建议的解决方案:
select *
from firstTable
where EXISTS (select 1
from secondTable
where firstTable.name = secondTable.name
and firstTable.age = secondTable.age))
但是如果我在两个表上都有“john - null”,它会将它们视为未知(既不相等也不不相等)。我知道原因,但我确实需要他们平等。
我需要这样做的原因是为了将当前索引值保留到查询结果中。