Sql 在结果集中获取重复的行 pkid, empid, skillid 1 2 3 2 2 4 3 2 5 4 5 6 select pkid, empid from yo

Sql 在结果集中获取重复的行 pkid, empid, skillid 1 2 3 2 2 4 3 2 5 4 5 6 select pkid, empid from yo,sql,sql-server,tsql,Sql,Sql Server,Tsql,在结果集中获取重复的行 pkid, empid, skillid 1 2 3 2 2 4 3 2 5 4 5 6 select pkid, empid from your_table where skillid in (3,4,5) or skillid in (4,5,6) group by pkid, empid having count(distinct skillid) = 3 select e

在结果集中获取重复的行

pkid, empid, skillid
 1     2       3
 2     2       4
 3     2       5
 4     5       6
select pkid, empid
from your_table
where skillid in (3,4,5) or skillid in (4,5,6) 
group by pkid, empid
having count(distinct skillid) = 3
select empid
from t1
where skillid in (3, 4, 5) 
    or skillid in (4, 5, 6) 
group by empid
having count(distinct skillid) = 3
create table Table1 (pkid int constraint PK_Table1 primary key, empid int, skillid int) insert into table1 values (1,2,1) insert into table1 values (2,2,2) insert into table1 values (3,2,3) insert into table1 values (4,3,1) SELECT empid FROM ( SELECT empid, sum(t.s1) as s1, sum(t.s2) as s2, sum(t.s3) as s3, sum(t.s4) as s4, sum(t.s5) as s5, sum(t.s6) as s6 FROM ( select empid, 1 s1, 0 s2, 0 s3, 0 s4, 0 s5, 0 s6 from table1 where skillid = 1 union all select empid, 0, 1, 0, 0, 0, 0 from table1 where skillid = 2 union all select empid, 0, 0, 1, 0, 0, 0 from table1 where skillid = 3 union all select empid, 0, 0, 0, 1, 0, 0 from table1 where skillid = 4 union all select empid, 0, 0, 0, 0, 1, 0 from table1 where skillid = 5 union all select empid, 0, 0, 0, 0, 0, 1 from table1 where skillid = 6 ) t GROUP BY t.empid ) tt WHERE (tt.s1 = 1 and tt.s2 = 1 and tt.s3 = 1) or (tt.s4 = 1 and tt.s5=1 and tt.s6=1)
SELECT empid, 345 AS skillset
FROM your_table
WHERE skillid IN (3,4,5)
GROUP BY empid
HAVING COUNT(DISTINCT skillid) = 3
UNION ALL
SELECT empid, 456 AS skillset
FROM your_table
WHERE skillid IN (4,5,6)
GROUP BY empid
HAVING COUNT(DISTINCT skillid) = 3
select *
from employee e
where (     exists ( select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 3 )
        and exists ( select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 4 )
        and exists ( select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 5 )
      )
   OR (     exists ( select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 4 )
        and exists ( select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 5 )
        and exists ( select * from employee_skill es3 on es3.empid = e.empid and es3.skillid = 6 )
      )
select *
from employee e
where exists ( select * from employee_skill es3 on es3.empid = e.empid and es3.skillid =    4       )
  and exists ( select * from employee_skill es3 on es3.empid = e.empid and es3.skillid =    5       )
  and exists ( select * from employee_skill es3 on es3.empid = e.empid and es3.skillid in ( 3 , 6 ) )
select *
from employee e
left join employee_skill es3 on es3.empid = e.empid and es3.skillid = 3
left join employee_skill es4 on es4.empid = e.empid and es4.skillid = 4
left join employee_skill es5 on es5.empid = e.empid and es5.skillid = 5
left join employee_skill es6 on es6.empid = e.empid and es6.skillid = 6
where es4.empid is not null
  and es5.empid is not null
  and (    es3.empid is not null
        OR es6.empid is not null
      )