日期:2014-05-17  浏览次数:20370 次

求一SQL,字段多值的问题
TABLE
F1 F2
A1,BB,C ABC,CC
AA BC,QQQ
A1A ABCD,ZZ


现在F1=A1,F2=ABC
求WHERE条件的写法,定位到一条记录。


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

select * from [TABLE]
where charindex(',A1,',','+F1+',')>0
and charindex(',ABC,',','+F2+',')>0

------解决方案--------------------
SQL code
select * from TB where charindex('A1,',F1+',')>0 And Charindex('ABC,',F2+',')>0

------解决方案--------------------
SQL code
if object_id(N'[t]') is not null drop table [t]
go
create table t([F1] varchar(10),[F2] varchar(10))
go
insert into t
select 'A1,BB,C','ABC,CC' union all
select 'AA','BC,QQQ' union all
select 'A1A','ABCD,ZZ'
go

declare @f1 varchar(10),@f2 varchar(10)
select @f1='A1',@f2='ABC'
select * from t
where charindex(','+@f1+',',','+[F1]+',')>0 and charindex(','+@f2+',',','+[F2]+',')>0

/*

(3 row(s) affected)
F1         F2
---------- ----------
A1,BB,C    ABC,CC

(1 row(s) affected)

*/

------解决方案--------------------
探讨
引用:

SQL code

select * from [TABLE]
where charindex(',A1,',','+F1+',')>0
and charindex(',ABC,',','+F2+',')>0


这样子的话,那第2条记录,F1=AA就找不到了?