tags:

views:

46

answers:

5

Hello all. Never came across this before...

SELECT ".TBL_USERS.".username, 
       ".TBL_USERS.".id,  <-----------|
       ".TBL_COMMENTS.".creator,      |
       ".TBL_COMMENTS.".comment,      |- same column name
       ".TBL_COMMENTS.".date,         |
       ".TBL_COMMENTS.".id  <---------|

As you can see, I am selecting two id columns... When i take them in PHP:

$userid = $row['id'];

How will I be able to realize which one is which?

+3  A: 

This is one reason why ID is poor choice for an ID column. You will have to alias your column names to know which is which.

HLGEM
heh - I thought of "Intelligent Design" when I read "ID"... =)
OMG Ponies
+5  A: 

You need to use a column alias, with AS keyword:

Example:

(id) AS id1.........(id) AS id2

Now from your query result, you can get them like this:

$userid1 = $row['id1'];
$userid2 = $row['id2'];

More Info:

http://dev.mysql.com/doc/refman/5.0/en/select.html

Sarfraz
Perfect answer!
Luke
+1  A: 

USe AS to rename one of the ID column in the table.

shamittomar
+1  A: 

Remember... "AS" is not required for mysql, you can...

SELECT ".TBL_USERS.".username, 
       ".TBL_USERS.".id id1,  <-----------|
       ".TBL_COMMENTS.".creator,      |
       ".TBL_COMMENTS.".comment,      |- same column name
       ".TBL_COMMENTS.".date,         |
       ".TBL_COMMENTS.".id id2 <----------|
el_quick
Thankyou for this, for other types of SQL it is required then?
Luke
+1: Nice tip :)
shamittomar
I am not sure if other types of SQL requires "AS", It would be good if somebody else will help us.
el_quick