当列名相同时,对连接表的 PDO 查询 [英] PDO Query on joined tables when column names are the same

查看:28
本文介绍了当列名相同时,对连接表的 PDO 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 PDO 在一对连接表上运行查询,如下所示:

I'm running a query via PDO on a pair of joined tables like so:

SELECT table1.id, table2.id, table1.foo, table1.bar 
FROM table1 INNER JOIN table2 ON table1.bar = table2.id;

两个表都有一个 id 列,所以当我运行 fetchAll() 时,关联数组只包含一个 id 字段.这是因为第一个被第二个覆盖.

Both tables have an id column so when I run fetchAll() the associative array only contains one id field. This is because the first is overwritten by the second.

有没有办法同时获得两个 id 字段?也许通过在数组键中包含表名...

Is there a way to obtain both id fields? Perhaps by having the table name included in the array keys...

推荐答案

使用别名

SELECT table1.id as t1id, table2.id as t2id
--etc.

这篇关于当列名相同时,对连接表的 PDO 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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