检索结果时如何解析模棱两可的列名? [英] How to resolve ambiguous column names when retrieving results?
本文介绍了检索结果时如何解析模棱两可的列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库中有两个表:
I have two tables in my database:
具有列的NEWS表:
-
id
-新闻ID -
user
-作者的用户ID)
id
- the news iduser
- the user id of the author)
USERS表带有列:
USERS table with columns:
-
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屋!
查看全文