declare @tab table(id int,name varchar(20))
insert @tab values(1,'12345')
insert @tab values(2,'aa')
insert @tab values(3,'hhh45')
insert @tab values(4,'dd09')select * from @tab where 
(charindex('a',name)>0) or 
(charindex('d',name)>0)
select * from @tab where 
(charindex('1',name)>0) or 
(charindex('9',name)>0)

解决方案 »

  1.   

    我是这个意思,有表t1
    id  f1
    1   **
    2   **
    3   **
    ..  ..想查询id为指定值v得记录,如id =1,5,7等,关键是v的值有多个,且不固定,请问怎么做?
      

  2.   

    用charindex或substring可以实现,效率不高!
      

  3.   

    如果v是一个数组,是否需要用怎样找出id等于v的各个元素的所有记录呢?有点像产品库和购买人的模式,某个人购买了多个商品,怎样查询该人购买的所有产品?
      

  4.   

    create table aa (a int,b varchar(20))
    go
    insert into aa select 1,'aa'
    union all select 2,'bb'
    union all select 3,'cc'
    union all select 4,'dd'
    union all select 5,'ee'
    union all select 6,'ff'
    union all select 7,'gg'
    union all select 8,'hh'
    union all select 9,'ii'
    go
    declare @nums varchar(50)
    set @nums='1,5,7'
    set @nums=','+@nums+','
    select * from aa where charindex(','+cast(a as varchar(10))+',',@nums)>0
    --测试结果
    a           b                    
    ----------- -------------------- 
    1           aa
    5           ee
    7           gg(所影响的行数为 3 行)