SQL查询返回的值不存在于表中(括号中的集合)

SQL查询返回的值不存在于表中(括号中的集合),sql,tsql,Sql,Tsql,需要获取不在表中的值,并在TSQL查询中正确枚举 餐桌城市 创建表格城市(Id int,City nvarchar(max)) 插入城市值 (1,“纽约”), (2"莫斯科"),, (3,'墨西哥'), (4,“瓜拉兰普尔”) 现在我需要提取集合中存在的城市 (‘洛杉矶’、‘莫斯科’、‘奥斯陆’)但不存在于城市表中。 所以我需要结果: 洛杉矶 奥尔索 我知道最简单的方法就是使用temp表 declare @temp table (City nvarchar (max)) insert int

需要获取不在表中的值,并在TSQL查询中正确枚举

餐桌城市

创建表格城市(Id int,City nvarchar(max))
插入城市值
(1,“纽约”),
(2"莫斯科"),,
(3,'墨西哥'),
(4,“瓜拉兰普尔”)

现在我需要提取集合中存在的城市 (‘洛杉矶’、‘莫斯科’、‘奥斯陆’)但不存在于城市表中。 所以我需要结果:

  • 洛杉矶
  • 奥尔索
我知道最简单的方法就是使用temp表

declare @temp table (City nvarchar (max))
insert into @temp values
(Los Angeles),
(Moscow),
(Oslo)

Select City from @temp
where City not in (Select City from Cities)

但是是否有办法避免临时表?

您可以使用一个包含
值的派生表

SELECT *
       FROM (VALUES ('Los Angeles'),
                    ('Moscow'),
                    ('Oslo')) given_cities (city)

       WHERE given_cities.city NOT IN (SELECT cities.city
                                              FROM cities);