简体   繁体   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

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_idskills表中可能有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.

相关问题 如何仅在一个表中的特定行与另一个表中的特定行基于id匹配的情况下回显名称? - How do I echo out a name -only if- specific rows in one table match specific rows in another based on an id? 在PHP和MySQL中仅将特定行从一个表复制到另一个表 - Copy only specific rows from one table to another in PHP and MySQL 如何使用自动递增的ID将多行插入一个表,然后插入另一个表? MySQL的 - How can I insert multiple rows into one table, then into another table using an auto incremented ID? mysql 如何使用联接基于另一个表中的行从一个表中获取mysql行? - How can I use joins to fetch mysql rows from one table based on rows that are found in another? 如何从一个表中选择被MySQL中的另一个表过滤的行? - How do I select rows from one table filtered by another table in MySQL? 与MySQL中不同数据库中的另一个表相比,如何从一个表中获取缺少的行? - How do i get the missing rows from a table compared to another table in different databases in MySQL? MySQL-从一个表中获取所有匹配条件和关系的特定行(如果有的话)来自另一表 - Mysql - Get all specific rows from one table matching criteria and relations if any from other table 如何将行从一个MySQL表复制到另一个表并基于变量更新值之一 - How do I copy rows from one MySQL table to another and update one of the values based on a variable 在mysql中,如何查找未在表中的其他行中引用其ID的行? - In mysql how do I find rows whose id are not referenced in any other rows in the table? 如何从具有一个唯一字段和另一字段的特定值的表中提取所有行? - How do I pull all rows from a table with one unique field and specific values for another field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM