如何将表与多个工会进行比较并加入?

时间:2015-09-07 01:46:41

标签: mysql sql join union

我试图找到仅存在于一个表中的行。大表名(包括所有行)是kwf_uploads,小表存在于许多名称中。我的目标是找到存在于kwf_uploads中但不存在于其他行中的行(此示例是没有关系的表中的主键和外键)。

我在sql中做了什么:

select id from
kwf_uploads
left join

(SELECT picture_id as id
FROM documents where picture_id is not null
UNION 
SELECT file_id as id
FROM books where file_id is not null
UNION 
SELECT picture_id as id
FROM employee where picture_id is not null
UNION 
SELECT file_id as id
FROM flightFiles where file_id is not null
UNION 
SELECT picture_id as id
FROM tasks where picture_id is not null
UNION 
SELECT picture_id as id
FROM trainingContentQuestions where picture_id is not null
UNION 
SELECT picture_id as id
FROM trainingQuestions where picture_id is not null) foo  

ON kwf_uploads.id = foo.id

SQL: cascade UNION and JOIN

上找到它

但它失败了,错误:SQL (1052): Column "id" in field list is ambiguous

我不想在每个表中使用join,因为我的联接不好,而且sql变得非常庞大且难以理解。我也尝试not exists没有任何结果。 我想我们可以找到更好的解决方案=)

来自select id from kwf_uploads查询的结果(9690行):

enter image description here

联合查询的结果(6096行):

enter image description here

结果我想看3594(9690减去6096)行。

3 个答案:

答案 0 :(得分:3)

UNION(单独使用,没有ALL)是一种“昂贵的”#34;操作

有另一种选择,不存在。这个结构是一个"半连接"我怀疑可能比工会方法便宜。

SELECT
      id
FROM kwf_uploads AS u
WHERE NOT EXISTS     (SELECT NULL FROM documents WHERE u.id = picture_id )
      AND NOT EXISTS (SELECT NULL FROM books WHERE u.id = file_id )
      AND NOT EXISTS (SELECT NULL FROM employee WHERE u.id = picture_id )
      AND NOT EXISTS (SELECT NULL FROM flightFiles WHERE u.id = file_id )
      AND NOT EXISTS (SELECT NULL FROM tasks WHERE u.id = picture_id )
      AND NOT EXISTS (SELECT NULL FROM trainingContentQuestions WHERE u.id = picture_id )
      AND NOT EXISTS (SELECT NULL FROM trainingQuestions WHERE u.id = picture_id )

答案 1 :(得分:1)

修复当前错误

带有正确结果集别名的前缀标识列。在这种情况下kwf_uploads.id。因为Id存在于两个结果集中。

获得所需的输出

添加Where foo.id is null。这将获取那些在连接中失败的ID,这些是你想要的ID。

  select kwf_uploads.id from
        kwf_uploads
        left join

        (SELECT picture_id as id
        FROM documents where picture_id is not null
        UNION 
        SELECT file_id as id
        FROM books where file_id is not null
        UNION 
        SELECT picture_id as id
        FROM employee where picture_id is not null
        UNION 
        SELECT file_id as id
        FROM flightFiles where file_id is not null
        UNION 
        SELECT picture_id as id
        FROM tasks where picture_id is not null
        UNION 
        SELECT picture_id as id
        FROM trainingContentQuestions where picture_id is not null
        UNION 
        SELECT picture_id as id
        FROM trainingQuestions where picture_id is not null) foo  

        ON kwf_uploads.id = foo.id
        where foo.id is null

答案 2 :(得分:1)

在DarkKnight旁边回答:

您不需要在每个where上添加union select。最糟糕的情况是,您从NULL获得一个UNION,但不会出现在LEFT JOIN

select kwf_uploads.id 
from kwf_uploads
left join
    (
    SELECT picture_id as id FROM documents
    UNION 
    SELECT file_id as id FROM books
    UNION 
    SELECT picture_id as id FROM employee
    UNION 
    SELECT file_id as id FROM flightFiles
    UNION 
    SELECT picture_id as id FROM tasks
    UNION 
    SELECT picture_id as id FROM trainingContentQuestions 
    UNION 
    SELECT picture_id as id FROM trainingQuestions
    ) foo      
ON kwf_uploads.id = foo.id
where foo.id is null