这是我的样品表
CREATE TABLE agentskills
(
id INT PRIMARY KEY NOT NULL IDENTITY,
skilluserid INT,
skillid INT NOT NULL,
skilltypeid INT,
priority INT DEFAULT 0
)
这些是我的示例数据
INSERT INTO AGENTSKILLS (skilluserid , skillid, SKILLTYPEID,priority ) VALUES (1,21,1,1)
INSERT INTO AGENTSKILLS (skilluserid , skillid, SKILLTYPEID,priority ) VALUES (1,22,1,1)
INSERT INTO AGENTSKILLS (skilluserid , skillid, SKILLTYPEID,priority ) VALUES (2,23,1,1)
INSERT INTO AGENTSKILLS (skilluserid , skillid, SKILLTYPEID,priority ) VALUES (2,24,1,1)
INSERT INTO AGENTSKILLS (skilluserid , skillid, SKILLTYPEID,priority ) VALUES (3,21,1,1)
INSERT INTO AGENTSKILLS (skilluserid , skillid, SKILLTYPEID,priority ) VALUES (3,22,1,1)
此处,“ skilluserid”是具有技能“ skillid”的用户的ID。
我如何获取具有技术技能组合的用户/ skilluserid?
例如:获取具有技能21和22或22和21或21,22,23的用户。具有21和22的任意组合。
答案 0 :(得分:2)
您可以使用聚合和having
:
select skilluserid
from agentskills
where skillid in (21, 22)
group by skilluserid
having count(distinct skillid) = 2; -- "2" = size of list in where clause
答案 1 :(得分:1)
如果需要选择所有列,则可以使用exist
select t1.* from agentskills t1
where exists ( select 1 from agentskills t2 where t1.skilluserid=t2.skilluserid
and skillid in (21, 22)
having count(distinct skillid) = 2
) and skillid in (21, 22)
答案 2 :(得分:1)
您可以使用相交,
select skilluserid
from agentskills
where skillid =21
INTERSECT
select skilluserid
from agentskills
where skillid =22