【问题标题】:How to merge multiple tables in MYSQl which have two columns in common如何在MYSQl中合并具有两列共同的多个表
【发布时间】:2015-01-23 07:12:06
【问题描述】:

我有四个表 table_1、table_2、table_3 和 table_4。他们四个都有这样的列:

table_1: age_grp, gender, height;
table_2: age_grp, gender, weight;
table_3:  age_grp, gender, shoesize;
table_4: age_group, gender, BMI;

我想创建一个包含列的新表:

age_grp, gender, height, weight, shoesize, BMI

我只想合并那些在所有表中age_grp and gender 相同的列。知道怎么做吗?

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    这可以通过INNER JOIN轻松完成:

    SELECT table_1.*, table_2.*, table_3.*, table_4.* FROM table_1  
      INNER JOIN table_2 ON table_1.age_grp = table_2.age_grp  
        AND table_1.gender = table_2.gender
      INNER JOIN table_3 ON table_2.age_grp = table_3.age_grp  
        AND table_2.gender = table_3.gender
      INNER JOIN table_4 ON table_3.age_grp = table_4.age_grp  
        AND table_3.gender = table_4.gender
    

    如果您要求所有表中的所有数据在列中具有相同的值,您可以JOIN 任何表。

    请注意,您不应在实际生产脚本中使用*,明确使用列名。

    【讨论】:

    • 第三个和第四个,我可以在逗号后面加名字吗?
    • @VMAtm 我有类似的查询,想优化 LINQ 查询
    • @ArijitMukherjee 在使用 LINQ 时,您应该问一个不同的问题。但是JOINs 不能轻易优化。
    • @VMAtm 不能去参加这个对话聊天吗?
    • @VMAtm 非常感谢。请不要介意我问这个,但我只有一个小问题。现在我不希望age_grp 和gender 出现多次。我的意思是现在新的选择有像age_grpgenderheightage_grpgenderweight等行。
    【解决方案2】:

    您很可能无法通过纯匹配获得您想要的结果。例如,以下将创建您描述的表:

    insert into newtable 
        select t1.age_grp, t1.gender, t1.height, t2.weight, t3.shoesize, t4.BMI 
        from table_1 t1 
        inner join table_2 t2 on t1.age_grp = t2.age_grp 
            and t1.gender = t2.gender
        inner join table_3 t3 on t1.age_grp = t3.age_grp 
            and t1.gender = t3.gender
        inner join table_4 t4 on t1.age_grp = t4.age_grp 
            and t1.gender = t4.gender;
    

    问题是,如果有 ANY 项失败,您将得不到一行。您可以考虑改用外连接。

    【讨论】:

      【解决方案3】:

      尽管已经回答了这个请求,但我想为缺失值的情况添加一个答案,例如只为 age_grp/gender 对提供鞋码。

      对于连接解决方​​案,您需要 MySQL 不支持的 FULL OUTER JOIN。使用 LEFT 和/或 RIGHT OUTER JOIN 来模仿这一点对于多个表来说可能会很痛苦。

      这是一个使用 UNION ALL 和最终聚合的解决方案。

      create table mytable as
      select age_grp, gender, max(height) as height, max(weight) as weight, max(shoesize) as shoesize, max(bmi) as bmi
      from
      (
        select age_grp, gender, height, cast(null as unsigned integer) as weight, cast(null as unsigned integer) as shoesize, cast(null as unsigned integer) as bmi from table_1
        union all
        select age_grp, gender, cast(null as unsigned integer) as height, weight, cast(null as unsigned integer) as shoesize, cast(null as unsigned integer) as bmi from table_2
        union all
        select age_grp, gender, cast(null as unsigned integer) as height, cast(null as unsigned integer) as weight, shoesize, cast(null as unsigned integer) as bmi from table_3
        union all
        select age_group, gender, cast(null as unsigned integer) as height, cast(null as unsigned integer) as weight, cast(null as unsigned integer) as shoesize, bmi from table_4
      ) x
      group by age_grp, gender;
      

      我很惊讶CAST(NULL AS INT) 会导致语法错误,顺便说一句。我不得不把它改成CAST(NULL AS UNSIGNED INTEGER)

      SQL 小提琴:http://www.sqlfiddle.com/#!2/f4fa5c/1.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-25
        • 2021-03-15
        • 2014-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-27
        相关资源
        最近更新 更多