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

分隔字符,选出需要的行
SQL code
create table temp_T1
(
   ID int,
   U1 varchar(100),
   U2 varchar(100)
   
)
insert into temp_T1 select 1,'AA2,BB3,AA,AA4',''
insert into temp_T1 select 2,'DD1,DD2',''
insert into temp_T1 select 3,'CC1',''
insert into temp_T1 select 4,'EE8,AA',''
insert into temp_T1 select 5,'XX',''
insert into temp_T1 select 6,'','TT'
insert into temp_T1 select 7,'CCCC',''
insert into temp_T1 select 8,'','AA'
insert into temp_T1 select 9,'BXXX',''


select * from temp_T1


ID          U1                                                                                                   U2
----------- ---------------------------------------------------------------- ----------------------------------------------------------------
1           AA2,BB3,AA,AA4                                                                                       
2           DD1,DD2                                                                                              
3           CC1                                                                                                  
4           EE8,AA                                                                                               
5           XX                                                                                                   
6                                                                                                                TT
7           CCCC                                                                                                 
8                                                                                                                AA
9           BXXX                                                                                                 

(9 行受影响)


---------------------------------------------
找出U1栏中有AA的或者U2栏中没有AA的行。请问如何写这个SQL语句比较好呢?



------解决方案--------------------
','+U1+',' LIKE '%,AA,%'
------解决方案--------------------
SQL code

--漏写分隔符


select *
from tb
where ','+u1+',' like '%,AA,%' or ','+u2+',' not like '%,AA,%'