我有表1:
Id Program Price Age
12345 ABC 10 1
12345 CDE 23 3
12345 FGH 43 2
12346 ABC 5 4
12346 CDE 2 5
12367 CDE 10 6
和表2:
ID Program BestBefore
12345 ABC 2
12345 FGH 3
12346 ABC 1
我想要得到下表,
Id Program Price Age
12345 CDE 10 1
12346 CDE 2 5
12367 CDE 10 6
即从第一个表中获取 ID+Program 不在第二个表中的行。我正在使用 MS SQL Server Express 2012,我不想向原始数据库添加任何列。是否可以不创建临时变量?
最佳答案
有几种方法可以做到这一点,这里有一种使用 not contains
的方法:
select *
from table1 t1
where not exists (
select 1
from table2 t2
where t1.id = t2.id and t1.program = t2.program
)
关于sql - 从一个表中选择其中两列不存在于另一个表中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35857726/