MySQL Join Table具有相同的列名

时间:2015-05-18 03:04:19

标签: mysql sql

我想加入这两个具有相同列名的表,以获得最终结果,如下所示。我怎么能这样做?

表1,(主要: - 关键日期)

-------------------------------
|        date         | value |
-------------------------------
| 2015-05-16 03:21:46 |   2   |
-------------------------------

表2,(主要: - 关键日期)

-------------------------------
|        date         | value |
-------------------------------
| 2015-05-16 03:21:46 |   3   |
-------------------------------

期待最终结果

-------------------------------------------------------
|        date         | value(table1) | value(table2) |
-------------------------------------------------------
| 2015-05-16 03:21:46 |       2       |        3      |
-------------------------------------------------------

2 个答案:

答案 0 :(得分:3)

只需在填写列时添加表名:

SELECT date, table1.value as value1, table2.value as value2
    FROM table1
    JOIN table2 USING (date)

这会给你这个结果:

-----------------------------------------
|        date         | value1 | value2 |
-----------------------------------------
| 2015-05-16 03:21:46 |   2    |   3    |
-----------------------------------------

答案 1 :(得分:-1)

SELECT t1.date, t1.value as value1, t2.value as value2
FROM table1 t1
JOIN table2 t2 ON t1.date = t2.date