MySQL: SELECT * FROM table1, table2.... column1 AS newColumnName

qwerty picture qwerty · Jan 27, 2011 · Viewed 12.4k times · Source

I'm selecting data from two tables. And both of those tables have an ID column, and i need both of the ID columns returned after executing the Query. Is there any way to change the name of the ID column (from the second table i'm selecting from) to something else using AS?

I'm thinking something like this:

SELECT * FROM table1, table2 WHERE table2.id AS newAlias

Can I use the WHERE statement like that?

Answer

Chandu picture Chandu · Jan 27, 2011

You need to specify the alias for the column in the select list. e.g:

SELECT a.id AS table1_id, b.id AS table2_id, ....
  FROM table1 a, table2 b
 WHERE <YOUR CRITERIA>