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条件要如何写呢?
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,2,3类型证书的人员?结果是无
你这个事要两个sql,还是一个啊?
select id from t2 where type in (1,2,3) group by id having count(1)=3
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;
刚才测了一下
只要group by id就可以了
例如: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+")";
如查询同时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
两种写法性能感觉差不多
group by id having count(1) = ( length('1,3')-length(replace('1,3',',',''))+1 )
我后来这样写了