【问题标题】:MySQL Union for two tables, then merge in a third table where matched?MySQL Union for 两个表,然后在匹配的第三个表中合并?
【发布时间】:2011-02-03 22:14:38
【问题描述】:

我尝试将三个 MySQL 表查询到一个结果中。就我所知,我认为这是可能的,但我在最后一部分工作方面遇到了障碍。

基本上我有两个表(table_one 和 table_two),我正在尝试 UNION DISTINCT,它工作得很好。当我试图引入第三张桌子时,一切都崩溃了,并决定什么都不返回。我确定这是用户错误:)

这段代码可能没有把所有东西都放在正确的地方,所以我可能只需要在正确的方向上稍微推动一下。

SELECT
    part_code,
    name
FROM
    table_three

WHERE
    table_three.part_code = (

    SELECT
        part_code
    FROM
        table_one

    UNION DISTINCT
    SELECT
        part_code
    FROM
        table_two
)

ORDER BY
    name ASC

我很感激有人可以提供任何方向。

【问题讨论】:

    标签: mysql union distinct


    【解决方案1】:
    WHERE
        table_three.part_code IN(
                              ^^
    

    编辑
    以下是一些满足要求的替代方案:Gief 表 3 中的所有行,以便零件代码存在于表 1 或表 2 中

    select t3.part_code
          ,t3.name
      from table_three t3
     where part_code in(select t1.part_code from table_one t1)
        or part_code in(select t2.part_code from table_two t2);
    

    带有联合的派生表

    select t3.part_code
          ,t3.name
      from table_three t3
      join (select part_code from table_one 
             union
            select part_code from table_two
           ) t12
         on(t3.part_code = t12.part_code);
    

    与联合的内连接

    select t3.part_code
          ,t3.name
      from table_three t3
      join table_one   t1 on(t3.part_code = t1.part_code)
    union   
    select t3.part_code
          ,t3.name
      from table_three t3
      join table_two   t2 on(t3.part_code = t2.part_code);  
    

    奖金。我不知道我为什么这样做。

    select t3.part_code
          ,t3.name
      from table_three t3
      left join (select distinct part_code 
                          from table_one) t1 on(t3.part_code = t1.part_code)
      left join (select distinct part_code 
                          from table_two) t2 on(t3.part_code = t2.part_code)
     where t3.part_code = t1.part_code
        or t3.part_code = t2.part_code;
    

    让我知道他们是如何工作的。

    编辑 2。
    好的,试试下面的。它应该产生表 T1 和 T2 的并集。然后对于每一行,如果可以找到这样的零件代码,它将从 T3 中选择名称。

    如果 part_code 是所有表中的键,您可以改为使用UNION ALL

    select T12.part_code
          ,coalesce(T3.name, T12.name) as name
      from (select part_code, name from table_one T1 union   
            select part_code, name from table_two T2
           ) T12
      left join table_three T3 on(T1.part_code = T3.part_code);
    

    【讨论】:

    • 这看起来很有希望,但它让我在 phpMyAdmin 中出现了这个错误:#1064 - 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以了解正确的语法使用
    • 这是我现在得到的,上面的错误:SELECT part_code,name FROM table_three WHERE table_three.part_code = IN( SELECT part_code FROM table_one UNION DISTINCT ( SELECT part_code FROM table_two ) ) ORDER BY name ASC
    • @Will Ashworth,删除“IN”关键字前的等号
    • 天啊。那太简单了……第一个例子做到了。我怎么没试过?!对于这个很好的例子,你太棒了。我认为它会对其他人有所帮助,因为我试图尽可能为其他搜索此帖子的人提供描述。
    • 其实,也许我可以更清楚一点。无论如何,您的查询完美地解决了首次加入/合并信息的主要问题。表三是表一/二的补充,是可选的。如果 name 字段匹配添加,否则返回结果中的 name 字段将为空。表三是次要的,在这些示例中现在发生的情况是表一/二的数据正在从结果中删除,因为它以某种方式使用三作为主控。
    【解决方案2】:

    试试这样的:)

    SELECT
        part_code,
        name
    FROM 
        table_three
    
    WHERE
       table_three.part_code = (
    
    SELECT
        part_code
    FROM
        table_one
    
    UNION DISTINCT (
    SELECT
        part_code
    FROM
        table_two
    
     ))
    
    ORDER BY
       name ASC;
    

    【讨论】:

    • 也试过了。我收到 MySQL 错误 #1242 - 子查询在 phpMyAdmin 中返回多于 1 行。
    猜你喜欢
    • 2012-09-12
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 2020-08-05
    相关资源
    最近更新 更多