[英]In mysql how can I get only rows from one table which do not link to any rows in another table with a specific ID
I have two tables with the following structures (unnecessary columns trimmed out) 我有两个具有以下结构的表(删去了不必要的列)
----------------- ---------------------
| mod_personnel | | mod_skills |
| | | |
| - prs_id | | - prs_id |
| - name | | - skl_id |
----------------- | |
---------------------
There may be 0 to many rows in the skills
table for each prs_id
每个
prs_id
的skills
表中可能有0到许多行
What I want is all the personnel records which do NOT have an associated skill record with skill_id
1. In plain English "I want all the people who do not have the skill x". 我想要的是所有没有与
skill_id
1相关的技能记录的人员记录。 skill_id
“我希望所有不具备x技能的人”。
Currently, I have only been able to do it with the following nested select
. 目前,我只能使用以下嵌套的
select
来做到这一点。 But I am hoping to find a faster way. 但我希望找到一种更快的方法。
SELECT * FROM `mod_personnel` WHERE `prs_id` NOT IN (
SELECT `prs_id` FROM `mod_skills` WHERE `skl_id` = 1 )
Using a NOT EXISTS
might be faster. 使用
NOT EXISTS
可能会更快。
SELECT *
FROM `mod_personnel` p
WHERE NOT EXISTS (SELECT *
FROM `mod_skills` s
WHERE s.`prs_id` = p.`prs_id`
AND s.`skl_id` = 1 );
This may be faster: 这可能会更快:
SELECT `mod_personnel`.*
FROM `mod_personnel`
left outer join `mod_skills`
on `mod_skills`.`prs_id` = `mod_personnel`.`prs_id`
and `mod_skills`.`skl_id` = 1
WHERE `mod_skills`.`prs_id` is null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.