在连接查询中对具有相同名称的表字段表示如何获取两个字段的值


In join query to table field have same name means how 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") 
对于上面的查询,我需要两个表1中的值。

给出别名,

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

注意: 请不要在新代码中使用mysql_*函数。它们不再被维护,并被正式弃用。见红框?学习预处理语句,并使用PDO或MySQLi——本文将帮助您决定使用哪一种。如果你选择PDO,这里有一个很好的教程。

使用别名:

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")