从值列表中选择

时间:2020-09-24 12:08:57

标签: sql sql-server tsql subquery unpivot

所以很简单:

Select Val from MyTable where Val not in ('100','200','300'....)
  1. 如何编写查询,以便从列表中选择值。例如,我该如何做类似Select * from ('100','200','300'....)的事情,以使输出为:

    100    
    200    
    300    
    ...
    
  2. 此外,我该如何做类似select * from ('100','200','300'....) that are not in MyTable.Val列的事情?

2 个答案:

答案 0 :(得分:3)

如何处理MyTable.Val中不在('100','200','300'....)中的select *。

您可以使用values()来构建包含值列表的派生表,然后使用not exists来过滤表中找不到的值:

select v.*
from (values (100), (200), (300)) v(val)
where not exists (select 1 from mytable t where t.val = v.val)

答案 1 :(得分:1)

第1部分

Select * from (values ('100'),('200'),('300')) v(val);

第2部分

Select * 
from
  (values ('100'),('200'),('300')) v(val)
where not exists (select 1 from myTable t where t.val=v.val);