检索结果时如何解决不明确的列名? [英] How to resolve ambiguous column names when retrieving results?

查看:34
本文介绍了检索结果时如何解决不明确的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有两个表:

I have two tables in my database:

带有列的新闻表:

  • id - 新闻 ID
  • user - 作者的用户 ID)
  • id - the news id
  • user - the user id of the author)

带有列的用户表:

  • id - 用户 ID
  • id - the user id

我想执行这条SQL:

SELECT * FROM news JOIN users ON news.user = user.id 

当我在 PHP 中得到结果时,我想通过 $row['column-name'] 获取关联数组并获取列名.如何获取新闻 ID 和用户 ID,列名相同?

When I get the results in PHP I would like to get associative array and get column names by $row['column-name']. How do I get the news ID and the user ID, having the same column name?

推荐答案

您可以为您选择的列设置别名:

You can set aliases for the columns that you are selecting:

$query = 'SELECT news.id AS newsId, user.id AS userId, [OTHER FIELDS HERE] FROM news JOIN users ON news.user = user.id'

这篇关于检索结果时如何解决不明确的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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