table1
 id          name       orderid 
  1           a             0
  2           a             1,3
  3           a             2,1
  4           a             1,2
  5           a             2,3想查如下结果:
 当id=5时显示 (显示id在orderid中的记录)
 id          name       orderid 
  2           a             1,3
  3           a             2,1
请问大家怎么实现啊?
 

解决方案 »

  1.   

    declare @t table(id int,name varchar(5),orderid varchar(10))
    insert @t
    select  1 ,          'a' ,            '0' union all
    select  2 ,         'a'  ,          '1,3'  union all
    select  3 ,          'a' ,            '2,1' union all
    select  4 ,          'a' ,            '1,2' union all
    select  5 ,          'a' ,           '2,3'
    select * from @t 
    where charindex(','+cast(id as varchar)+',',','+
    (select orderid from @t where id=5)+',')<>0
      

  2.   

    select * from table1
    where charindex(cast(id as varchar),(select orderid from table1 where id=5))<>0