PHP MYSQL:如何从连接表中获取不明确的列结果 [英] PHP MYSQL: How to get ambiguous column result from joined table

查看:52
本文介绍了PHP MYSQL:如何从连接表中获取不明确的列结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我连接两个表并且结果集会有不明确的列名,我如何通过php检索到正确的列名?

If I am joining two tables and the result set will have ambiguous coloumn names, how can I retrieve the correct one through 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?

How can I get access to b.name and m.name from the $row array?

谢谢.

推荐答案

在您的 MySQL 查询中使用 AS 为其别名.例如:SELECT name AS display_name, name FROM users;

Alias it in your MySQL query with AS. For example: SELECT name AS display_name, name FROM users;

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

It's not in PHP, but that's the common way of doing it.

这篇关于PHP MYSQL:如何从连接表中获取不明确的列结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆