【问题标题】:use integer values in one table to look up values in another SQL table使用一个表中的整数值在另一个 SQL 表中查找值
【发布时间】:2020-06-25 18:09:13
【问题描述】:

我希望在一个表中使用整数值来查找另一个 SQL 表中的字符串值。 例如,我有两个表:

╔═════════╦═══════════╦═════════════════╦═══════════════════╗
║ CarType ║ CarColour ║ AbnormalCarType ║ AbnormalCarColour ║
╠═════════╬═══════════╬═════════════════╬═══════════════════╣
║       1 ║         1 ║ 1               ║ 2                 ║
║       1 ║         2 ║ null            ║ null              ║
║       2 ║         1 ║ 1               ║ 2                 ║
║       2 ║         2 ║ 1               ║ 1                 ║
╚═════════╩═══════════╩═════════════════╩═══════════════════╝

然后在我的另一个表中:

╔═══════════╦═══════════════╦═════════════╦═════════════════╦══╗
║ CarTypeId ║ CarTypeString ║ CarColourId ║ CarColourString ║  ║
╠═══════════╬═══════════════╬═════════════╬═════════════════╬══╣
║         1 ║ "Hyundai"     ║           1 ║ "Red"           ║  ║
║         1 ║ "Hyundai"     ║           2 ║ "Blue"          ║  ║
║         2 ║ "Toyota"      ║           1 ║ "Green"         ║  ║
║         2 ║ "Toyota"      ║           2 ║ "Yellow"        ║  ║
╚═══════════╩═══════════════╩═════════════╩═════════════════╩══╝

然后为输出

╔═════════╦═══════════╦═════════════════╦═══════════════════╗
║ CarType ║ CarColour ║ AbnormalCarType ║ AbnormalCarColour ║
╠═════════╬═══════════╬═════════════════╬═══════════════════╣
║ Hyundai ║ Red       ║ Hyundai         ║ Blue              ║
║ Hyundai ║ Blue      ║ null            ║ null              ║
║ Toyota  ║ Green     ║ Hyundai         ║ Blue              ║
║ Toyota  ║ Yellow    ║ Hyundai         ║ Red               ║
╚═════════╩═══════════╩═════════════════╩═══════════════════╝

我尝试了双重内连接,第一次是 CarType、CarColour,然后是 AbnormalCarType 和 AbnormalCarColour,但它没有产生我想要的结果。

非常感谢!

【问题讨论】:

    标签: mysql sql google-bigquery


    【解决方案1】:

    以下是 BigQuery 标准 SQL

    #standardSQL
    SELECT b.CarTypeString AS CarType, b.CarColourString AS CarColour, 
      c.CarTypeString AS AbnormalCarType, c.CarColourString AS AbnormalCarColour
    FROM `project.dataset.tableB` b  
    LEFT JOIN `project.dataset.tableA` a
    ON b.CarTypeId = a.CarType AND b.CarColourId = a.CarColour
    LEFT JOIN `project.dataset.tableB` c
    ON a.AbnormalCarType = c.CarTypeId AND a.AbnormalCarColour = c.CarColourId 
    

    如果适用于您示例中的示例数据

    WITH `project.dataset.tableA` AS (
      SELECT 1 CarType, 1 CarColour, 1 AbnormalCarType, 2 AbnormalCarColour UNION ALL
      SELECT 1, 2, NULL, NULL UNION ALL
      SELECT 2, 1, 1, 2 UNION ALL
      SELECT 2, 2, 1, 1 
    ), `project.dataset.tableB` AS (
      SELECT 1 CarTypeId, 'Hyundai' CarTypeString, 1 CarColourId, 'Red' CarColourString UNION ALL
      SELECT 1, 'Hyundai', 2, 'Blue' UNION ALL
      SELECT 2, 'Toyota', 1, 'Green' UNION ALL
      SELECT 2, 'Toyota', 2, 'Yellow' 
    )
    

    输出符合预期

    Row CarType CarColour   AbnormalCarType AbnormalCarColour    
    1   Hyundai Red         Hyundai         Blue     
    2   Hyundai Blue        null            null     
    3   Toyota  Green       Hyundai         Blue     
    4   Toyota  Yellow      Hyundai         Red  
    

    【讨论】:

    • 非常感谢您的回复!不幸的是,我的最小工作示例还不够好,这也不是我想要的。我已经编辑了我的开场白。如果您有时间再看一遍,那就太好了。
    • 顺便说一句,与此同时,我根据您的新样本数据测试了给出的答案(假设 AbnormalCarColour 归档实际上应该是 CarColourString 与原始样本一样)并且它仍然有效。无论如何 - 请考虑上述建议
    • 非常感谢再次回复并为之前道歉,以后发帖时会牢记。
    猜你喜欢
    • 1970-01-01
    • 2022-12-14
    • 2018-11-14
    • 2014-12-21
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 2020-10-10
    相关资源
    最近更新 更多