我使用三个表设计了以下技能数据库:
1)和2)只为人和技能分配数字。 3)为员工分配技能水平。
例如,表3中的第1 3 6行表示具有个人编号3的人具有技能编号1的知识,知识级别为6。
我想要的是找回同时具备技能的人
最好的方法是什么?我想首先选择满足条件1)的人,然后从满足条件2)的人中选择,然后从结果中选择符合条件的人3)。但是对于这种方法,我必须使用一些过程编程语言创建临时表并超越SQL。难道没有更好的方法吗?
答案 0 :(得分:2)
应该是一个相当直接的内部联接;
SELECT e.*
FROM employees e
JOIN skillmapping s1 ON s1.personnelnumber = e.personnelnumber
AND s1.skillid = 2
AND s1.skillevel = 3
JOIN skillmapping s2 ON s2.personnelnumber = e.personnelnumber
AND s2.skillid = 4
AND s2.skillevel = 5
JOIN skillmapping s3 ON s3.personnelnumber = e.personnelnumber
AND s3.skillid = 8
AND s3.skillevel = 2
答案 1 :(得分:1)
我会使用子查询来计算一个人的技能匹配数量,并确保它出现为三个:
SELECT name
FROM person
WHERE personnelnumber IN (SELECT personnelnumber
FROM skillmapping
WHERE (skillid = 2 AND skilllevel = 3) OR
(skillid = 4 AND skilllevel = 5) OR
(skillid = 8 AND skilllevel = 2)
GROUP BY personnelnumber
HAVING COUNT(*) = 3)
答案 2 :(得分:1)
QUERY(我使用EXISTS功能来检查该组合是否存在......):
SELECT * FROM Employees WHERE
EXISTS (SELECT * FROM skillmapping t2 WHERE t2.PersonnelNumber = Employees.PersonnelNumber AND SkillId = 2 AND t2.SkillLevel >= 3) --NOTE: lvl 3 or more!
AND EXISTS (SELECT * FROM skillmapping t2 WHERE t2.PersonnelNumber = Employees.PersonnelNumber AND SkillId 4 AND t2.SkillLevel >= 5) -- NOTE: lvl 5 or more
AND EXISTS (SELECT * FROM skillmapping t2 WHERE t2.PersonnelNumber = Employees.PersonnelNumber AND SkillId = 8 AND t2.SkillLevel >= 2) -- NOTE: lvl 2 or more