有一表结构:
编号 指定人 时间
id assign time
记录 001 test ..
002 test,test1 ...
003 test1,test2 ...
现在想从中选出指定人(assign)包含test值的记录,即想选出001和002记录来,这个sql 的条件怎么写,请大虾赐教,谢谢!
select * from dtl where ...... ?
------解决方案--------------------
select * from dtl
where assign = 'test' or assign like 'test,%' or assign like '%,test' or assign like '%,test,%'
------解决方案--------------------
- SQL code
select * from dtl where charindex(','+'test'+',' , ','+assign+',')>0
------解决方案--------------------
同意楼上的
------解决方案--------------------
- SQL code
有一表结构: 编号 指定人 时间 id assign time 记录 001 test .. 002 test,test1 ... 003 test1,test2 ... 方法一:select * from tb where ','+指定人+',' like '%,test,%'方法二:select * from tb where charindex(',test,' , ','+指定人+',') > 0
------解决方案--------------------
- SQL code
create table tb( 编号 varchar(10),指定人 varchar(20),时间 datetime)insert into tb values('001','test',null) insert into tb values('002','test,test1',null) insert into tb values('003','test1,test2',null) go--方法一:select * from tb where ','+指定人+',' like '%,test,%'/*编号 指定人 时间 ---------- -------------------- ------------------------------------------------------ 001 test NULL002 test,test1 NULL(所影响的行数为 2 行)*/--方法二:select * from tb where charindex(',test,' , ','+指定人+',') > 0 /*编号 指定人 时间 ---------- -------------------- ------------------------------------------------------ 001 test NULL002 test,test1 NULL(所影响的行数为 2 行)*/drop table tb
------解决方案--------------------
select * from tb where
contains(assign,'test') or contains(assign,',test') or contains(assign,'test,')