繁体   English   中英

在mysql中,我如何只从一个表中获取行,而该行不链接到具有特定ID的另一表中的任何行

[英]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

我有两个具有以下结构的表(删去了不必要的列)

-----------------   ---------------------
| mod_personnel |   | mod_skills        |
|               |   |                   |
| - prs_id      |   | - prs_id          |
| - name        |   | - skl_id          |
-----------------   |                   |
                    ---------------------

每个prs_idskills表中可能有0到许多行

我想要的是所有没有与skill_id 1相关的技能记录的人员记录。 skill_id “我希望所有不具备x技能的人”。

目前,我只能使用以下嵌套的select来做到这一点。 但我希望找到一种更快的方法。

SELECT * FROM `mod_personnel` WHERE `prs_id` NOT IN (
SELECT `prs_id` FROM `mod_skills` WHERE `skl_id` = 1 )

使用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 );

这可能会更快:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM