MySQL查询以获取不属于另一个表的所有项目

时间:2014-02-19 07:39:14

标签: mysql sql

我有一个问题,我几个小时都无法解决。

我有3张桌子:

  • Process

    +----+----------+----------------+--------+-------------+---------------+-------------+---------+------+
    | id |   name   |  description   | active | responsible | informByEmail | informBySms | remarks | icon |
    +----+----------+----------------+--------+-------------+---------------+-------------+---------+------+
    |  4 | Process1 | TestDecriptino |      1 |           0 |             0 |           0 | 0       | NULL |
    |  5 | Process2 | test           |      0 |           0 |             0 |           0 | test    | NULL |
    |  6 | Process3 | 12322          |      1 |           0 |             0 |           0 | 12322   | NULL |
    |  7 | Process4 | 222222222222   |      0 |           0 |             0 |           0 | 2222222 | NULL |
    |  9 | Process5 | sgdasad        |      1 |           0 |             1 |           0 | dhds    | NULL |
    +----+----------+----------------+--------+-------------+---------------+-------------+---------+------+
    
  • Systems

    +----+---------+-------------+--------+-------------------+---------------+-------------+---------+------+
    | id |  name   | description | active | responsibleUserId | informByEmail | informBySms | remarks | icon |
    +----+---------+-------------+--------+-------------------+---------------+-------------+---------+------+
    |  2 | Sistem1 | fdjgf       |      1 |                 1 |             1 |           1 | 0       | NULL |
    |  6 | Sistem2 | koam        |      0 |                 3 |             1 |           0 | SADGS   | NULL |
    +----+---------+-------------+--------+-------------------+---------------+-------------+---------+------+
    
  • Process_Systems

    +----+-----------+----------+--------+---------+
    | id | processId | systemId | active | remarks |
    +----+-----------+----------+--------+---------+
    |  4 |         4 |        2 |      1 | aa      |
    |  8 |         7 |        2 |      1 | aa      |
    | 11 |         9 |        2 |      1 | aa      |
    | 15 |         4 |        6 |      0 | aa      |
    +----+-----------+----------+--------+---------+
    

我有一个方法,processID是一个参数,必须以某种方式按该ID过滤所有Process_Systems,然后获取不属于Systems表的Process_Systems

1 个答案:

答案 0 :(得分:2)

  • 一种方法是使用NOT IN subquery

    SELECT   *
    FROM     Systems
    WHERE    id NOT IN (
               SELECT systemid
               FROM   Process_Systems
               WHERE  processId = ?
             );
    
  • 另一种方法是使用NOT EXISTS subquery

    SELECT   *
    FROM     Systems
    WHERE    NOT EXISTS (
               SELECT *
               FROM   Process_Systems
               WHERE  systemId = Systems.id
                  AND processId = ?
             );
    
  • 第三种方法是使用outer join

    SELECT   Systems.*
    FROM     Systems LEFT JOIN Process_Systems ps
          ON ps.systemId = Systems.id
         AND ps.processId = ?
    WHERE    ps.systemId IS NULL;
    

sqlfiddle上查看。

有关各自性能差异的分析,请参阅@ Quassnoi的博客文章: