select CompId from DataTable
where (ProdId='id1' and Qty=1) 
or (ProdId='id2' and Qty=2)
or (ProdId='id3' and Qty=3)

解决方案 »

  1.   

    select ...
    from DataTable 
    where [Group] = 'Group1' and ((ProdId=id1 and Qty=1) or (ProdId=id2 and Qty=2) or (ProdId=id3 and Qty=3))
      

  2.   


    我数据库中的数据这是样的
    CompId   ProdId    Qty
    comp1     id1      1
    comp1     id2      2
    comp1     id3      3
    comp2     id1      1
    comp2     id2      2   Group里的内容是随便定的,只是用来方便区分的,现在有
    ProdId   Qty
    id1      1
    id2      2
    id3      3
    这些数据,那么就要想查出comp1
      

  3.   

    DataTable是C#里的数据源,是读取文本文件获取来的,不是数据库里的表,不过也可认为是数据库里的表,但就是有两个表,Group和查询的数据无关
      

  4.   

    select b.CompId  from tb b inner join (
    select 'id1'ProdId,1 Qty union all
    select 'id2'ProdId,2 Qty union all
    select 'id1'ProdId,1 Qty)a on b.ProdId=a.ProdId and b.Qty=a.Qty  
      

  5.   

    这样还是不能够查出同时符合这三个条件的CompId,会把其它只符合其中一个条件的也查出来了
      

  6.   

    总算看懂你的意思了.select * from tb where comp1 in 
    (
    select CompId from
    (
    select distinct CompId from tb where ProdId = 'id1' and Qty = 1
    union all
    select distinct CompId from tb where ProdId = 'id2' and Qty = 2
    union all
    select distinct CompId from tb where ProdId = 'id3' and Qty = 3
    ) t group by CompId having count(*) = 3
    )
      

  7.   

    create table tb(CompId varchar(10), ProdId varchar(10),   Qty int)
    insert into tb values('comp1',    'id1' ,     1 )
    insert into tb values('comp1',    'id2' ,     2 )
    insert into tb values('comp1',    'id3' ,     3 )
    insert into tb values('comp2',    'id1' ,     1 )
    insert into tb values('comp2',    'id2' ,     2 )
    goselect CompId from
    (
    select distinct CompId from tb where ProdId = 'id1' and Qty = 1
    union all
    select distinct CompId from tb where ProdId = 'id2' and Qty = 2
    union all
    select distinct CompId from tb where ProdId = 'id3' and Qty = 3
    ) t group by CompId having count(*) = 3
    /*
    CompId     
    ---------- 
    comp1(所影响的行数为 1 行)
    */select * from tb where CompId in 
    (
    select CompId from
    (
    select distinct CompId from tb where ProdId = 'id1' and Qty = 1
    union all
    select distinct CompId from tb where ProdId = 'id2' and Qty = 2
    union all
    select distinct CompId from tb where ProdId = 'id3' and Qty = 3
    ) t group by CompId having count(*) = 3
    )
    /*
    CompId     ProdId     Qty         
    ---------- ---------- ----------- 
    comp1      id1        1
    comp1      id2        2
    comp1      id3        3(所影响的行数为 3 行)
    */drop table tb
      

  8.   

    好了,解决了,谢谢各位了,用group by CompId having count(*) = 3就可以了