PHP/MySQL: Getting Multiple Columns With the Same Name in Join Query Without Aliases?(PHP/MySQL:在无别名的联接查询中获取多个同名列?)
问题描述
我有两张桌子.一个用于用户,一个用于帖子.users 表有以下字段:
I have two tables. One for users and one for posts. The users table has the following fields:
posts 表有以下字段:
The posts table has the following fields:
当我使用如下查询时:
并使用 PDO 获取结果:
And fetch the results using PDO:
返回的数组会覆盖所有具有相同名称的列,例如 id、created_at、modified_at
,如下所示:
The returned array overwrites all the columns with the same names like id, created_at, modified_at
like this:
用户的 id
字段未显示,而是被帖子的 id
覆盖.created_at & 也是如此.modified_at 字段.
The id
field for the user is not shown, instead overwritten by the id
of the post. Same goes for the created_at & modified_at fields.
我相信我可以通过使用别名或类似的东西来解决这个问题:
I believe I can solve this problem by either using aliases or something like this:
或类似的东西,但我想知道是否有一种方法可以做到这一点,而无需每次我想区分两个同名的列时手动写出每一列的名称或别名?是否可以做类似的事情:
Or something similar, but I was wondering if there was a way to do this without manually writing out the name of every single column or an alias for every time I want to distinguish between two columns of the same name? Is it possible to do something like:
让它自动为每一列使用别名?或者有没有其他更快/更方便的方法来做到这一点?或者我可以这样做而无需自己定义任何别名吗?
And have it automatically use aliases for every column? Or is there any other faster/more convenient way to do this? Or can I do this without having to define any aliases myself?
谢谢
推荐答案
我觉得在这种情况下别名是完美的
I think alias is perfect in this case
为了节省一些打字时间,请使用表格别名:
To save you some typing time, use table aliases:
这篇关于PHP/MySQL:在无别名的联接查询中获取多个同名列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!