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

12.8k Views Asked by At

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?

2

There are 2 best solutions below

5
On

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>
0
On

@qwerty

You can write:

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