Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Join Table which have had same column name

Tags:

sql

mysql

I would like to join up this two tables which have had same columns name to get end results as follow. How can I do so?

Table 1, (Primary :- key date)

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

Table 2, (Primary :- key date)

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

Expecting end result

-------------------------------------------------------
|        date         | value(table1) | value(table2) |
-------------------------------------------------------
| 2015-05-16 03:21:46 |       2       |        3      |
-------------------------------------------------------
like image 535
Eric T Avatar asked May 18 '15 03:05

Eric T


2 Answers

Just add the table names when you address the columns:

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

This will give you this result:

-----------------------------------------
|        date         | value1 | value2 |
-----------------------------------------
| 2015-05-16 03:21:46 |   2    |   3    |
-----------------------------------------
like image 60
TimoStaudinger Avatar answered Oct 22 '22 10:10

TimoStaudinger


SELECT t1.date, t1.value as value1, t2.value as value2
FROM table1 t1
JOIN table2 t2 ON t1.date = t2.date
like image 44
Tony Vu Avatar answered Oct 22 '22 11:10

Tony Vu