Page 1 of 1

How do I SELECT same column name from multiple tables

Posted: 15 Nov 2014, 15:34
by Marius
I have 2 tables with same column names in my database which are supposed to provide content in 2 languages.
This is how I query for one table (this query is executed correctly):

Code: Select all

$sql = "SELECT * FROM tab_en WHERE time > $from";
However, when I try to do it for 2 tables:

Code: Select all

$sql = "SELECT * FROM tab_en, tab_es WHERE time > $from";
I am getting the error. What am doing wrong?

How do I SELECT same column name from multiple tables

Posted: 15 Nov 2014, 15:44
by MarPlo
The mysql is confused when you select columns with same name. The solution is to use Alias ( AS ).

Code: Select all

$sql = "SELECT en.col1 AS c1en, en.col2 AS c2en, es.col1 AS c1es, es.col2 AS c2es FROM tab_en AS en, tab_es AS es WHERE time > $from";
- The same technique can be applied when you use JOIN with tables that have columns with same names.

Code: Select all

$sql = "SELECT en.col1 AS c1en, en.col2 AS c2en, es.col1 AS c1es, es.col2 AS c2es FROM tab_en AS en
LEFT JOIN tab_es AS es ON en.id = es.id
WHERE en.time > $from";