[英]MySQL: Select all records where it matches multiple IDs
This is based on a question that was answered here . 这是基于此处回答的问题。
I need to export relational records to a flat CSV file. 我需要将关系记录导出到平面CSV文件中。 It displays a list of employees that match two skills. 它显示匹配两种技能的员工列表。 The export needs to have a separate line for each skill. 导出需要为每个技能单独一行。 For example: 例如:
EMPID NAME SKILLNAME LEVEL 1 Fred Flintstone PHP Basic 1 Fred Flintstone JQuery Advanced 3 Steve Jobs PHP Basic 3 Steve Jobs JQuery Advanced
Here's the tables/records and a modified version of the previous answer: SQL Fiddle 这是上一个答案的表/记录和修改后的版本: SQL Fiddle
I have tried a few variations but no luck. 我尝试了一些变化但没有运气。
Thanks 谢谢
You need a sub query to find the employees that have more than 2 skills. 您需要一个子查询来查找具有2种以上技能的员工。 And then you just list the details of those skills by joining it to the Emp_skills table. 然后,只需将其加入Emp_skills表中即可列出这些技能的详细信息。
SELECT Emp_SKills.*
FROM Emp_Skills
JOIN (
SELECT COUNT(*) as skills, Emp_ID
FROM Emp_Skills
GROUP BY Emp_ID
HAVING skills > 1
) as mult ON mult.Emp_ID = Emp_Skills.Emp_ID
ORDER BY Emp_SKills.Emp_ID
There you go with a subquery 那里有一个子查询
SELECT a.Emp_ID, b.Name, c.Name as skname, a.Level
FROM Emp_Skills a
LEFT JOIN Employee b
ON a.Emp_ID = b.ID
LEFT JOIN Skill c
ON a.Skill_ID = c.ID
WHERE (
SELECT count(d.Emp_Id) as total
FROM Emp_Skills d
WHERE d.Emp_id = a.Emp_id
GROUP BY Emp_id
) > 1
ORDER BY a.Emp_ID ASC
Based on op new info 根据操作新信息
SELECT a.Emp_ID, b.Name, c.Name as skname, a.Level
FROM Emp_Skills a
LEFT JOIN Employee b
ON a.Emp_ID = b.ID
LEFT JOIN Skill c
ON a.Skill_ID = c.ID
WHERE (
SELECT count(d.Emp_Id) as total
FROM Emp_Skills d
WHERE d.Emp_id = a.Emp_id
GROUP BY Emp_id
) > 1
AND a.Skill_ID IN ('1', '2')
ORDER BY a.Emp_ID ASC
select a.emp_id, b.name, c.name as skname, a.level
from emp_skills a
join employee b
on a.emp_id = b.id
join skill c
on a.skill_id = c.id
where b.id in (select emp_id from emp_skills where skill_id = '1')
and b.id in (select emp_id from emp_skills where skill_id = '2')
and a.skill_id in ('1', '2')
* SQL FIDDLE: http://sqlfiddle.com/#!2/35fe1/40/0 * * SQL字段: http ://sqlfiddle.com/#!2/35fe1/40/0 *
Thanks to Fabio, I was able to find the solution based on his answer. 感谢Fabio,我能够根据他的答案找到解决方案。 The final query is: 最终查询是:
SELECT a.Emp_ID, b.Name, c.Name as skname, a.Level
FROM Emp_Skills a
LEFT JOIN Employee b
ON a.Emp_ID = b.ID
LEFT JOIN Skill c
ON a.Skill_ID = c.ID
WHERE (
SELECT count(d.Emp_Id) as total
FROM Emp_Skills d
WHERE d.Emp_id = a.Emp_id
GROUP BY Emp_id
AND d.Skill_ID IN ('1', '2')
) = 2
AND a.Skill_ID IN ('1', '2')
ORDER BY a.Emp_ID ASC
I have added 我已经添加了
AND d.Skill_ID IN ('1', '2')so it only finds employees with the selected skills. 因此它只会找到具有所选技能的员工。 I am not concerned with the skills I am searching for, not the total number of skills an employee has. 我不在乎我正在寻找的技能,而不在乎员工拥有的技能总数。 I have also changed '> 1' to '= 2'. 我也将“> 1”更改为“ = 2”。 That will change with the number of skills being search for. 这将随着所寻找技能的数量而改变。 So if I am looking for an employee with 4 particular skills, it would be '= 4'. 因此,如果我正在寻找具有4种特殊技能的员工,那将是'= 4'。
Thanks everyone. 感谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.