现有一个表,存储某些设备的构成状况,表结构大概如下:mid    ...  comp  ....
----------------------------
1           aaa
1           bbb
2           bbb
2           ddd
3           aaa
4           ddd
5           aaa
5           bbb
这里id是设备的编号, comp是该设备里使用的硬件的编号, 现在要实现一个查询,目的是找出使用了指定硬件的设备记录.比方说,指定的硬件为aaa,bbb 则返回的应该是如下:mid    ...  comp  minfo ....
----------------------------
1           aaa
1           bbb
5           aaa
5           bbb如果指定的硬件为aaa 则返回的应该是如下:mid    ...  comp  minfo ....
----------------------------
1           aaa
3           aaa
5           aaa不知道该怎么实现才比较好,先谢谢了

解决方案 »

  1.   

    create table t(mid int,comp varchar(4))
    insert into t select  
    1,           'aaa' union all select 
    1,           'bbb' union all select 
    2,           'bbb' union all select 
    2,           'ddd' union all select 
    3,           'aaa' union all select 
    4,           'ddd' union all select 
    5,           'aaa' union all select 
    5,           'bbb'
    go
    declare @s varchar(100),@i int,@sql varchar(8000)
    set @s='aaa,bbb'
    set @s=@s+','
    set @i=1
    set @sql=''
    while charindex(',',@s,@i)>0
    select @sql=@sql+' and exists(select 1 from t where mid=a.mid and comp='''+substring(@s,@i,charindex(',',@s,@i)-@i)+''')',@i=charindex(',',@s,@i)+1
    --print @sql
    exec('select * from t a where 1=1'+@sql)
    --结果
    mid         comp 
    ----------- ---- 
    1           aaa
    1           bbb
    5           aaa
    5           bbb
      

  2.   

    --修改
    declare @s varchar(100),@i int,@sql varchar(8000)
    set @s='aaa'
    set @s=@s+','
    set @i=1
    set @sql=''
    while charindex(',',@s,@i)>0
    select @sql=@sql+' and exists(select 1 from t where mid=a.mid and comp='''+substring(@s,@i,charindex(',',@s,@i)-@i)+''')',@i=charindex(',',@s,@i)+1
    --print @sql
    exec('select * from t a where charindex(comp+'','','''+@s+''')>0'+@sql)
      

  3.   

    create table t(mid int,comp varchar(4))
    insert into t select  
    1,           'aaa' union all select 
    1,           'bbb' union all select 
    2,           'bbb' union all select 
    2,           'ddd' union all select 
    3,           'aaa' union all select 
    4,           'ddd' union all select 
    5,           'aaa' union all select 
    5,           'bbb'create proc dbo.proc_display(@str varchar(200))
    as
    begin
    declare @int integer
    set nocount on
    select @str as ID into #temp1
    select @int=charindex(',',@str)
    if @int=0
    begin
    insert into #temp1
    select @str
    -- select * from #temp1
    end
    else
    begin
    insert into #temp1
    select left(@str,@int-1)
    set @str=right(@str,len(@str)-@int)
    set @int=charindex(',',@str)
    while @int<>0
    begin
    insert into #temp1
    select left(@str,@int)
    select @str=right(@str,len(@str)-@int)
    select @int=charindex(',',@str)
    end
    insert into #temp1
    select @str
    end
    --select * from #temp1
    select distinct * from t where exists(select 1 from #temp1 where ID=t.comp)
    order by mid,comp
    endproc_display 'aaa,bbb'dbo.proc_display 'aaa'
      

  4.   

    当为aaa,bbb时结果为:
    --------------- 
    1 aaa
    1 bbb
    2 bbb
    3 aaa
    5 aaa
    5 bbbLZ给得数据是不是有点问题呀?