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,339
  • 3
  • 23
  • 41
nagaking
  • 59
  • 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 stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
Rikesh
  • 25,621
  • 14
  • 77
  • 86
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,535
  • 3
  • 19
  • 29