【问题标题】:In join query to table field have same name means how get the two fields value?在对表字段的连接查询中具有相同的名称意味着如何获取两个字段的值?
【发布时间】:2013-03-26 08:18:06
【问题描述】:

我使用连接查询从具有相同字段名称的两个表中检索值。如何获取这两个字段的值?

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 的值。

【问题讨论】:

  • SELECT table1.Name AS table1Name, table2.Name AS table2Name

标签: php mysql phpmyadmin


【解决方案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

【讨论】:

  • 连接查询可以使用三张表吗?任何例子:-)
  • @nagaking 您可以在查询中使用n 连接数。如果您在加入新问题时遇到任何困难。
【解决方案2】:

使用别名:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    相关资源
    最近更新 更多