0

I used a join query to retrieve values from two tables that have the same field name. How can i get the two fields value?

mysql_query("SELECT table1.Name, table2.Name 
FROM table1
INNER JOIN table2
ON table1.Id=table2.userid
ORDER BY table1.Id DECS LIMIT 5") 

With the above query, I need values from both table1.Name and table2.Name.

juco
  • 6,331
  • 3
  • 25
  • 42
nagaking
  • 57
  • 1
  • 1
  • 7

2 Answers2

1

Give alias,

SELECT table1.Name as table1Name, table2.Name as table2Name
FROM table1
INNER JOIN table2
ON table1.Id=table2.userid
ORDER BY table1.Id DECS LIMIT 5

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
1

Use alias:

mysql_query("SELECT table1.Name as table1_name, table2.Name as table2_name 
FROM table1
INNER JOIN table2
ON table1.Id=table2.userid
ORDER BY table1.Id DECS LIMIT 5") 
m4t1t0
  • 5,669
  • 3
  • 22
  • 30