我使用连接查询从两个具有相同字段名称的表中检索值。我怎样才能获得两个字段的值?
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的值。
答案 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,并使用PDO或MySQLi - 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")