在连接查询到表字段有同名意味着如何获取两个字段的值?

时间:2013-04-04 10:01:25

标签: php mysql phpmyadmin

我使用连接查询从两个具有相同字段名称的表中检索值。我怎样才能获得两个字段的值?

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

通过上面的查询,我需要来自table1.Name和table2.Name的值。

2 个答案:

答案 0 :(得分:1)

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

注意Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分:1)

使用别名:

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