【问题标题】:MySql - Duplicate Columns When Joining Multiple tables to Main TableMySql - 将多个表连接到主表时重复列
【发布时间】:2015-01-08 21:24:40
【问题描述】:

背景
我得到了一堆看起来像这样的数据,大约 200 多列宽: 名称|地址|等...|值 1|值 2|代码 1|代码 2|代码重复 7 次|值 3|值重复 200 次....|值 200

它们包括用于破译编码值的定义列表,例如:U6 = 本地限制和 U7 = 超过 100 次

所以我将它加载到 mysql 中,因为他们想要从编码值的定义列表中交换值的报告。但是,并非主表中的所有单元格都有数据,有些是空白的。

问题
因此,在构建我的 select 语句时,我通常会使用左连接并且没问题,但是我需要多个左连接才能在需要时交换 8 个定义列表,多个左连接给了我很多额外的字段,遇到了麻烦这个。

主表被调用
原始数据

保存所有定义列表的表被命名为:
国家
起源
语言
偏好
HAS_VEHICLE
教育
MARITAL_STATUS
职业
技术代码
类型
收入

上面的这些表格只是有定义的表格。 225 表中的所有其他字段都是静态的并且通常是唯一的。我敢肯定,它可以正常化,但一次转换一份报告将需要大量的努力。这就是为什么我只使用那些通过定义列表具有人类无法识别的代码的原因。

MY QUERY  

SELECT `raw_data`.`id_raw_data`,
    `raw_data`.`id`,
    `raw_data`.`first_name`,
    `raw_data`.`last_name`,
    `raw_data`.`OTHER_COLUMNS_AS_NEEDED`,
    `country`.`longname` as `country`,
    `origin`.`longname` as `origin`,
    `language`.`longname` as `language`,
    `preference`.`longname` as `preference`,
    `has_vehicle`.`longname` as `vehichle_type`,
    `education`.`longname` as `education`,
    `marital_status`.`longname` as `marital_status`,
    `occupation`.`longname` as `occupation`,
    `techcode`.`longname` as `tech_group`,
    `typestat`.`longname` as `typecode`,
    `income`.`longname` as `income`,
FROM `raw_data` 
left join `country`
    on `raw_data`.`countrycode` = `country`.`shortname`
left join `origin`
    on `raw_data`.`origincode` = `origin`.`shortname`
left join `language`
    on `raw_data`.`languagecode` = `origin`.`language`
left join `preference`
    on `raw_data`.`preferencecode` = `preference`.`shortname`
left join `has_vehicle`
    on `raw_data`.`has_vehiclecode` = `has_vehicle`.`shortname`
left join `education`
    on `raw_data`.`educationcode` = `education`.`shortname`
left join `marital_status`
    on `raw_data`.`marital_statuscode` = `marital_status`.`shortname`
left join `occupation`
    on `raw_data`.`occupationcode` = `occupation`.`shortname`
left join `techcode`
    on `raw_data`.`techcodecode` = `techcode`.`shortname`
left join `typecode`
    on `raw_data`.`typestatcode` = `typestat`.`shortname`
left join `income`
    on `raw_data`.`incomecode` = `income`.`shortname`

我已经进行了一些搜索,似乎都使用了某种形式的子查询或涉及加入自身的问题。我很确定这与大量 raw_data 表中没有值的列有关,因此不匹配,但需要帮助。

这看起来很接近,但是如果已经有太多的连接,我的查询就会超时,这对于我所有的查找来说似乎更多的工作:Removing duplicates from result of multiple join on tables with different columns in MySQL

感谢您的帮助,

大卫

【问题讨论】:

    标签: mysql sql left-join


    【解决方案1】:

    如果其他人想知道,我发现问题根本不在于 sql,这对我的目的来说很好。

    相反,定义表中的数据具有一些不唯一的值,因此在定义重复定义的情况下,结果会返回额外的行。

    【讨论】:

      猜你喜欢
      • 2017-07-23
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      相关资源
      最近更新 更多