我在MySQL中有2个表(“ Alpha”和“ Beta”)。我需要加入他们。不幸的是,当尝试下一个SQL代码时出现错误。我需要一些建议。
错误:
Duplicate column name 'TIME_KEY'
SQL:
SELECT * FROM Alpha A
LEFT JOIN Beta B ON A.TIME_KEY = B.TIME_KEY
表格“ Alpha”
TIME_KEY | FIRST_NAME
---------------------|------------
2018-07-01 00:00:00 | Bob
2018-07-01 01:00:00 | Jon
2018-07-01 02:00:00 | Olivia
2018-07-01 03:00:00 | Emily
2018-07-01 04:00:00 | Isla
表格“测试版”
TIME_KEY | LAST_NAME
---------------------|------------
2018-07-01 00:00:00 | Johnson
我想要下一个结果:
TIME_KEY | FIRST_NAME | LAST_NAME
---------------------|------------|-----------
2018-07-01 00:00:00 | Bob | Johnson
2018-07-01 01:00:00 | Jon | NULL
2018-07-01 02:00:00 | Olivia | NULL
2018-07-01 03:00:00 | Emily | NULL
2018-07-01 04:00:00 | Isla | NULL
答案 0 :(得分:3)
具体指定列
SELECT A.Time_Key, A.First_Name, B.Last_Name
FROM Alpha A
LEFT JOIN Beta B ON A.TIME_KEY = B.TIME_KEY
如果您想让两个Time_Key都给他们起别名
SELECT A.Time_Key, B.Time_Key as TimeKey2, A.First_Name, B.Last_Name
FROM Alpha A
LEFT JOIN Beta B ON A.TIME_KEY = B.TIME_KEY