内部联接以获取所有属性

时间:2014-05-02 09:11:32

标签: mysql sql inner-join

我有两张桌子:

albums (idalbum, idauthor, idcompositor, idfeat)
people (id, firstname, last name)

我目前的查询:

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

我想用查询做什么:

[Album], [Author[First Name, Last Name]], [Compositor[First Name, Last Name]], [Feat[First Name, Last Name]]

我的问题:
我有一个错误:SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

1 个答案:

答案 0 :(得分:4)

你需要在你的join语句之后更正你的查询where子句,并且你没有使用正确的表名,根据所讨论的结构,你可以将你的查询写成

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