I am trying to retrieve 'date_and_time' field from two different tables of a mysql database using join query. Since the field name in both tables are same, the key name in the return result of an associated array being overwritten by the new key. How to change the key name if it exists, or is there any possibility to add table name along with field name as a key of associative array.
1 Answer
Ok, just for the people checking these post later: In SQL, tables & columns can be aliased: To fetch two columns which originally have the same name, it's easy to 'rename' them:
SELECT tableA.column AS yourColumnAlias, tableB.column as differentAlias FROM ...
will return a result-set in the form of
---------------------------------------------------------------------
|yourColumnAlias |differentAlias |
|value from tableA.column |value from tableB.column |
P.S.: In complex Joins, this also helps to keep Queries readable, as Tables can also be aliased.
SELECT tableA.column AS yourColumnAlias, tableB.column as differentAlias ...