当前位置: 代码迷 >> Sql Server >> 求一SQL,字段多值的有关问题
  详细解决方案

求一SQL,字段多值的有关问题

热度:6   发布时间:2016-04-27 11:32:35.0
求一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+',')>0and 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]gocreate table t([F1] varchar(10),[F2] varchar(10))goinsert into tselect 'A1,BB,C','ABC,CC' union allselect 'AA','BC,QQQ' union allselect 'A1A','ABCD,ZZ'godeclare @f1 varchar(10),@f2 varchar(10)select @f1='A1',@f2='ABC'select * from twhere charindex(',[email protected]+',',','+[F1]+',')>0 and charindex(',[email protected]+',',','+[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就找不到了?
  相关解决方案