PHP MYSQL:如何从连接表中获取不明确的列结果

时间:2010-06-30 02:10:44

标签: php mysql

如果我加入两个表并且结果集的名称不明确,我怎样才能通过php检索正确的名称?

示例:

$results = mysql_query("SELECT b.name, m.name FROM Brands as b INNER JOIN Models as m ON b.id = m.brand_id", $connection);

while ($row = mysql_fetch_array($results)) {
  printf("Name: %s", $row["name"]);
}

如何从$ row数组访问b.name和m.name?

谢谢。

2 个答案:

答案 0 :(得分:5)

使用AS在MySQL查询中将其别名。例如:SELECT name AS display_name, name FROM users;

它不是在PHP中,但这是常用的方法。

答案 1 :(得分:2)

使用as

为每个字段设置别名
    $results = mysql_query("SELECT b.name as bname, m.name as mname FROM Brands as b INNER JOIN Models as m ON b.id = m.brand_id", $connection);

    while ($row = mysql_fetch_array($results)) {
  printf("Name: %s", $row["bname"]);

}