日期:2014-05-18  浏览次数:20493 次

請教一查詢方法
我想在表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