读书人

教一查方法,该怎么处理

发布时间: 2012-01-21 21:31:43 作者: rapoo

教一查方法
我想在表testTable中的testField位中搜索是否有等於A或B或C的.
故我:
SELECT testField FROM testTable WHERE testField IN ( 'A ', 'B ', 'C ')
果出了有A和B,有C,如何C示到果集中?
(注意,testTable.testField中有包含有C件,只是件中有入C,目的是要示入的件中有的,而里有的值)
假在料中有A-Z多,其中缺少C和S.
可以入件如 IN ( 'A ', 'B ', 'C ',, 'S ',,, 'Z ')
用一SQL句示中有的C和S,如何做?

[解决办法]
列里不包括c和s怎么能找出这个列。
[解决办法]
建立条件序列表
create table #temp
(aa varchar(50)
)
insert into #temp
select 'A ' union all select 'B ' union all select 'C ' union all select 'D ' union all select 'E '


这个是主表--缺少E
create table #temp2
(aa varchar(50)
)
insert into #temp2
select 'A ' union all select 'B ' union all select 'C ' union all select 'D '

在条件表中查询出主表缺少的那个:
select * from #temp a where not exists(select 1 from #temp2 where aa=a.aa)
----
E
[解决办法]
借楼上 rookie_one 的:

select a.aa,
case when b.aa is null then '不存在 ' else '存在 ' end
from #temp a
left outer join #temp2 b
on a.aa = b.aa

读书人网 >SQL Server

热点推荐