【发布时间】: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