2张表
t1 人员基础信息表 id唯一
id name
1111 偶尔
2222 里t2 人员证书表 id+type唯一
id type(证书类型) paper
1111 1 证书名称1
1111 2 证书名称2
2222 3 证书名3
2222 1 证书名4例如,查询同时拥有1,2类型证书的人员  结果是1111
 查询同时拥有1,2,3类型证书的人员?结果是无
这样的sql条件要如何写呢?

解决方案 »

  1.   

    查询同时拥有1,2类型证书的人员 结果是1111
    查询同时拥有1,2,3类型证书的人员?结果是无
    你这个事要两个sql,还是一个啊?
      

  2.   


    select id from t2 where type in (1,2,3) group by id having count(1)=3
      

  3.   

    select id
    from t2
    where type in (1,2)
    group by id,type
    having count(id)=2;select id
    from t2
    where type in (1,2,3)
    group by id,type
    having count(id)=3;
      

  4.   

    sorry
    刚才测了一下
    只要group by id就可以了
      

  5.   

    你这个参数最好传两个参数到sql里。一个是id的字符串,因为你的type是字符型的,所以id必须是类似这种格式:'01','02','03',最好在传一个参数就是id的个数,这样拼接sql就很简单了。
    例如:string str = "'01','03'";
                int cnt = 2;
                string sqlstr = "select * from t1 where t1.id in (select id from t2 where type in (" + str +") group by id having count(1) =" + cnt+")";
      

  6.   

    最直接接的方法就是求交集。SQL的交集运算符intersect性能不错.
    如查询同时1,2,3的情况:select id from t2 where type=1
    intersect
    select id from t2 where type=2
    intersect
    select id from t2 where type=3借楼上计数也OK:select id from t2 where type in (1,2,3) group by id having count(*)>=3
    两种写法性能感觉差不多
      

  7.   

    select id  from t where type in (1,3) 
            group by id having count(1) = ( length('1,3')-length(replace('1,3',',',''))+1 )
    我后来这样写了