如何实现 "包含" 查询 ???????????????????????急!!!!!!!!!!!
表 如下, "数值内容" 的每个数字用 "、"号隔开  名称              数值内容(字符型 )
 aaa               1、11、110、210
 bbb               3、11、210、111
1,我想查询 出 "数值内容" 中  包含 有  1 并且  包含 有  11 的记录 。结果如下 : 名称              数值内容(字符型 )
 aaa               1、11、110、210
2,我想查询 出 "数值内容" 中条件:   (包含 有  1 并且  包含 有  11)   或  包含 有  210         的记录 。结果如下 : 名称              数值内容(字符型 )
aaa               1、11、110、210
bbb               3、11、210、111
请问这两个 SQL 语句 怎样写????????

解决方案 »

  1.   

    select * from tb where 数值内容 like '%1%'
    union select * from tb where 数值内容 like '%11%'
    union select * from tb where 数值内容 like '%210%'--效率高--或着
    select * from tb where 数值内容 like '%1%'or 数值内容 like '%11%' or 数值内容 like '%210%'
    效率低
      

  2.   

    用 like  不行呀 !!!!例如: 我想查 包含 有  1 的的数字select * from tb where 数值内容 like '%1%'
    这样会把有 110、11  的记录也查出来!!!!
      

  3.   

    select * from tb where 数值内容 like '%、1、%'
    union select * from tb where 数值内容 like '1、%'
    union select * from tb where 数值内容 like '%、1'
    union select * from tb where 数值内容 like '%、11、%'
    union select * from tb where 数值内容 like '%、11'
    union select * from tb where 数值内容 like '11、%'
    union select * from tb where 数值内容 like '%、210、%'
    union select * from tb where 数值内容 like '%、210'
    union select * from tb where 数值内容 like '210、%'
      

  4.   

    可以用T-sql的字符串处理函数。
      

  5.   

    T-sql好象没有直接处理字符串包含的函数要自己写
      

  6.   

    select * from tb  where charindex('、1、','、'+数值内容+'、')>0
    union select * from tb  where charindex('、11、','、'+数值内容+'、')>0
    union select * from tb  where charindex('、210、','、'+数值内容+'、')>0
    这样写也可以
      

  7.   

    或者这样select * from tb  where patindex('%、1、%','、'+数值内容+'、')>0
    union select * from tb  where patindex('%、11、%','、'+数值内容+'、')>0
    union select * from tb  where patindex('%、210、%','、'+数值内容+'、')>0
    我就知道这点方法
    不要就要写函数判断了
      

  8.   


    create table tb1(a varchar(100),b varchar(100))insert into tb1 select 'aaa','1、11、110、210'
    insert into tb1 select 'bbb','3、11、210、111'--1 SELECT *  from tb1
    where patindex('%[^0-9]1[^0-9]%',' '+b+' ')>0 
    and patindex('%[^0-9]11[^0-9]%',' '+b+' ')>0--2SELECT * from tb1
    where (patindex('%[^0-9]1[^0-9]%',' '+b+' ')>0 
    and patindex('%[^0-9]11[^0-9]%',' '+b+' ')>0)
    or patindex('%[^0-9]210[^0-9]%',' '+b+' ')>0
    drop table tb1
      

  9.   

    declare @t  table(mc varchar(6), sznr varchar(60))
    declare @t1 table(sznr varchar(60))insert into @t
    select   'aaa' ,              '1、11、110、210'  union 
    select   'bbb' ,              '3、11、210、111'  
    select * from @tdeclare @n int, @n1 int , @n2 int ,@n_con1 int , @n_con2 intdeclare cursor1 scroll cursor for select sznr from @t
    open cursor1declare @s  varchar(8000)
    declare @s1 varchar(8000)
    select @s1 = ''
    select @s  = ''
    fetch next from cursor1 into @swhile (@@fetch_status = 0)
      begin 
         
         select @n1 = charindex('、',@s,1)
    print @n1
         select @n2 = 1
         while @n1  > 0
            begin
              select @s1 = substring(@s,@n2,@n1-@n2 )
              if @s1 = '1' 
                 begin 
                   select @n_con1 = 1
                 end 
              if @s1 = '11'
                begin 
                   select @n_con2 = 1
                end
              
              if(@n_con1 = 1 and @n_con2 = 1 ) 
                begin 
                   insert into @t1
                   select @s 
                   goto T1
                end
              
               select @n2 = @n1 + 1
               select @n1 = charindex('、',@s,@n2)
            end 
        T1:
            select @n_con1 = 0
            select @n_con2 = 0
            fetch next from cursor1 into @s
       end
    close cursor1
    deallocate cursor1      select * from @t
          where sznr in 
         (select * from @t1)