考虑表 Address ,其中包含 Country、State 和其他数据字段。我想获取除 Country,State 组合为 (US, IL), (US,LA), (IND,DEL) 的所有记录
查询就像
Select * from Address a
where not exists
(
select Country,State
(select 'US' as Country, 'IL' as State
union
select 'US' as Country, 'LA' as State
union
select 'IND' as Country, 'DEL' as State
) e
where e.Country != a.Country and e.State != a.state
)
如何轻松实现(用简单的子查询替换国家,联合状态组合)?由于总数据不是很大,我现在最不关心性能。
我知道我可以创建表变量,使用插入语法在其中添加所有文字组合,并使用表变量表示不存在,但我觉得这对于小要求来说是多余的(不存在于 2 个变量上)。