我的数据表里面的数据格式:         id              Code
         1               周一上午;周一下午;周三下午;周五下午
         2               周一上午;周日上午;周五下午;周日下午
         3               周一上午;周三下午;周五下午;周日下午          现在需要用查询语句准确知道 :周一上午;周五下午;周日下午的数据...  最好别用like
                   显示结果         (and 关系时  周一上午;周五下午;周日下午)         2               周一上午;周日上午;周五下午;周日下午
         3               周一上午;周三下午;周五下午;周日下午
       显示结果         (or 关系时  周一上午;周五下午;周日下午)
         1               周一上午;周一下午;周三下午;周五下午
         2               周一上午;周日上午;周五下午;周日下午
         3               周一上午;周三下午;周五下午;周日下午

解决方案 »

  1.   

    select * from tb where charindex('周一上午',code)>0 and charindex('周五下午',code)>0 and charindex('周日下午',code)>0 
    select * from tb where charindex('周一上午',code)>0 or charindex('周五下午',code)>0 or charindex('周日下午',code)>0 
      

  2.   


    我的查询条件是   where code='周一上午;周五下午;周日下午' 然后自动匹配出来 而不是一个一个的来写charindex
      

  3.   

    select * from tb where charindex(';'+'周一上午;周五下午;周日下午'+';',';'+code+';')>0 
      

  4.   


     
    5把满足一定条件的数据用逗号分隔这可能也是一个很常用的语句了,经常出现在一对多的关系中对外展示,要求把子表中的数据取出来用逗号或者其他符号分隔开/*把满足 t2.NodeID=t1.NodeID的tableA 的字段NodeName 以逗号分隔开合并为一个字段输出*/select *,stuff((select ',' + t1.NodeName from tableA t1,tableB t2
                    where   t2.NodeID=t1.NodeID
                    for xml path('')) , 1 , 1 , '')  as text
    from tableA
    http://user.qzone.qq.com/270769564/infocenter
      

  5.   

    create table tb(id int,Code nvarchar(100))
    insert into tb select 1,'周一上午;周一下午;周三下午;周五下午'
    insert into tb select 2,'周一上午;周日上午;周五下午;周日下午'
    insert into tb select 3,'周一上午;周三下午;周五下午;周日下午'
    go
    declare @col varchar(1000)
    set @col='周一上午;周五下午;周日下午'
    ;with cte as(
    select substring(@col,number,charindex(';',@col+';',number+1)-number)c from master..spt_values
    where type='p' and number<=len(@col) and substring(@col,number,1)<>';' and substring(';'+@col,number,1)=';'
    )
    select * from tb a where exists(select 1 from cte where charindex(c,a.code)>0 having count(*)=(select count(*) from cte))
    --上面这句是 and 的,改用下面这句则是 or 的.
    --select * from tb a where exists(select 1 from cte where charindex(c,a.code)>0)
    go
    drop table tb
    /*
    id          Code
    ----------- ----------------------------------------------------------------------------------------------------
    2           周一上午;周日上午;周五下午;周日下午
    3           周一上午;周三下午;周五下午;周日下午(2 行受影响)*/