日期:2014-05-18 浏览次数:20647 次
select * from tb where (case when F1='A' then 1 else 0 end)+ (case when F3='B' then 1 else 0 end)+ (case when F4 in ('A','B') then 1 else 0 end)+ (case when F6='A' then 1 else 0 end) between 1 and 3
------解决方案--------------------
use Tempdb go --> --> if not object_id(N'Tempdb..#T') is null drop table #T Go Create table #T([ID] int,[F1] nvarchar(1),[F2] nvarchar(1),[F3] nvarchar(1),[F4] nvarchar(1),[F5] nvarchar(1),[F6] nvarchar(1)) Insert #T select 1,N'A',N'A',N'B',N'C',N'B',N'A' union all select 2,N'B',N'A',N'B',N'C',N'A',N'C' union all select 3,N'C',N'B',N'B',N'A',N'B',N'C' union all select 4,N'B',N'B',N'C',N'A',N'A',N'B' Go Select * from #T AS a WHERE (F1='A' OR F3='B' OR F4 IN('B','C') OR f6 IN('A')) AND NOT (F1='A' AND F3='B' and F4 IN('B','C') AND f6 IN('A')) /* ID F1 F2 F3 F4 F5 F6 2 B A B C A C 3 C B B A B C */