Candidates
CandidateId | Skill |
---|---|
1 | Sql |
1 | Dw |
1 | ssis |
2 | ssis |
2 | sql |
2 | vb |
Jobs
JobId | SkillReq |
---|---|
3 | Sql |
3 | Dw |
4 | ssis |
4 | sql |
4 | vb |
Expected output would be, Job_ID need the respective candidate ID with exact skill set match.
建表
CREATE TABLE Candidates (CandidateId INT, Skill VARCHAR(100));
INSERT INTO Candidates VALUES (1,'Sql'), (1,'Dw'), (1,'ssis'), (2, 'ssis'), (2,'sql'), (2,'vb');
CREATE TABLE Jobs (JobId INT, SkillReq VARCHAR(100));
INSERT INTO Jobs VALUES (3,'Sql'), (3,'Dw'), (4,'ssis'), (4,'sql'), (4,'vb');
SELECT a.jid AS JobId, a.cid AS CandidateId FROM
(SELECT J.JobId AS jid, C.CandidateId AS cid, count(1) AS count FROM Jobs J, Candidates C WHERE J.SkillReq=C.Skill GROUP BY J.JobId, C.CandidateId) AS a
INNER JOIN
(SELECT JobId AS jid, count(1) AS count FROM Jobs GROUP BY JobId) AS b
ON a.jid = b.jid AND a.count = b.count;
比如集合A={a, b}, 集合B={0, 1, 2},则二者的笛卡尔积为 [(a,0), (a,1), (a,2), (b,0), (b,1), (b,2)]
SELECT J.JobId AS jid, C.CandidateId AS cid, count(1) AS count FROM Jobs J, Candidates C GROUP BY J.JobId, C.CandidateId
举个例子,JobId=3 包括(Sql, Dw),CandidateId=1 包括(Sql, Dw, ssis),两者的笛卡尔积应该是: (Sql, Sql), (Sql, Dw), (Sql,ssis); (Dw, Sql), (Dw, Dw), (Dw, ssis)。
总计数量是6,为了得到技能匹配的数量2,须加限定条件:
SELECT J.JobId AS jid, C.CandidateId AS cid, count(1) AS count FROM Jobs J, Candidates C WHERE J.SkillReq=C.Skill GROUP BY J.JobId, C.CandidateId
此子句将得到技能匹配的情况,:
JobId | CandidateId | skill |
---|---|---|
3 | 1 | Sql |
3 | 1 | Dw |
3 | 2 | Sql |
4 | 1 | ssis |
4 | 1 | Sql |
4 | 2 | ssis |
4 | 2 | vb |
我们可以据此得到对应的匹配数量,比如 3-1:2, 3-2:1, 4-1:2, 4-2:2
SELECT JobId AS jid, count(1) AS count FROM Jobs GROUP BY JobId;
SELECT a.jid AS JobId, a.cid AS CandidateId FROM
(SELECT J.JobId AS jid, C.CandidateId AS cid, count(1) AS count FROM Jobs J, Candidates C WHERE J.SkillReq=C.Skill GROUP BY J.JobId, C.CandidateId) AS a
INNER JOIN
(SELECT JobId AS jid, count(1) AS count FROM Jobs GROUP BY JobId) AS b
ON a.jid = b.jid AND a.count = b.count;
(SELECT JobId, LOWER(GROUP_CONCAT(Distinct SkillReq ORDER BY SkillReq ASC SEPARATOR ',')) AS JS
FROM Jobs GROUP BY JobId)
AS J
(SELECT CandidateId, LOWER(GROUP_CONCAT(DISTINCT Skill ORDER BY Skill ASC SEPARATOR ',')) AS CS
FROM Candidates GROUP BY CandidateId)
AS C
SELECT J.JobId, C.CandidateId FROM
(SELECT JobId, LOWER(GROUP_CONCAT(DISTINCT SkillReq ORDER BY SkillReq ASC SEPARATOR ',')) AS JS FROM Jobs GROUP BY JobId) AS J
INNER JOIN
(SELECT CandidateId, LOWER(GROUP_CONCAT(DISTINCT Skill ORDER BY Skill ASC SEPARATOR ',')) AS CS FROM Candidates GROUP BY CandidateId) AS C
ON J.JS = C.CS;
注:主要是用到了函数 group_concat; 另外,加 lower 函数的原因是,去除大小写干扰。