多个内部联接擦除属性 [英] Multiple Inner Join erase attributes

查看:40
本文介绍了多个内部联接擦除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子:

相册(相册,idauthor,idcompositor,idfeat)

albums (idalbum, idauthor, idcompositor, idfeat)

人(身份证,名字,姓氏)

people (id, firstname, last name)

我的查询

SELECT * FROM albums  a
INNER JOIN people p ON a.idauthor = p.id
INNER JOIN people p1 ON a.idcompositor = p1.id
INNER JOIN people p2 ON a.idfeat = p2.id
where a.idalbum=:id

我想要什么:

Album,
p[First Name, Last Name],
p1[First Name, Last Name],
p2[First Name, Last Name]

我的问题:

当我print_r查询时,我只有一个名字和一个姓氏,即"p2"值.

When i print_r the Query i just have one first name and one last name, the "p2" values.

预先感谢您:)

推荐答案

您的问题是每个列都具有相同的名称.当您的结果转换为PHP数组时,后面的列将使用相同的索引/列名覆盖第一列.您必须为每个列赋予唯一的标识符,才能访问每个单个值.

Your problem is that each column has the same name. When your result is converted to an PHP array the later columns overwrite the first column with the same index / column name. You have to give each column a unique identifier, to access each single value.

SELECT *, p.firstname as p_firstname, p.lastname AS p_lastname,
p1.first_name AS p1_firstname, [...]
FROM albums  a
INNER JOIN people p ON a.idauthor = p.id
INNER JOIN people p1 ON a.idcompositor = p1.id
INNER JOIN people p2 ON a.idfeat = p2.id
where a.idalbum=:id

作为替代方案,您可以使用另一种访存样式,该样式将按其列号插入列到数组中.(如果您使用的是PDO,请参见 http://www.php.net/manual/en/pdostatement.fetch.php )

As an alternative you can use another fetch style, which inserts the columns by its column number into the array. (If you are using PDO, see http://www.php.net/manual/en/pdostatement.fetch.php)

这篇关于多个内部联接擦除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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