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

sql server 里面的包含怎么写?
字段A 字段B
A 结束
A 开始
A 开始
A 开始
A 开始
A 开始
B 开始
B 开始
B 开始
C 开始
C 开始
C 开始
C 开始

语句需要把字段A对应的字段B中不包含 结束的对应的记录集提取出来,
结果如下:



B 开始
B 开始
B 开始
C 开始
C 开始
C 开始
C 开始


------解决方案--------------------
SQL code

--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([字段A] varchar(1),[字段B] varchar(4))
insert [test]
select 'A','结束' union all
select 'A','开始' union all
select 'A','开始' union all
select 'A','开始' union all
select 'A','开始' union all
select 'A','开始' union all
select 'B','开始' union all
select 'B','开始' union all
select 'B','开始' union all
select 'C','开始' union all
select 'C','开始' union all
select 'C','开始' union all
select 'C','开始'

select * from test a
where not exists(select 1 from test b
    where a.字段A=b.字段A and b.字段B='结束')
/*
字段A    字段B
B    开始
B    开始
B    开始
C    开始
C    开始
C    开始
C    开始
*/