【问题标题】:Inner Join to get all attributes内连接获取所有属性
【发布时间】:2014-05-02 09:11:32
【问题描述】:

我有 2 张桌子:

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;

【问题讨论】:

  • 你的auteurs 桌子在哪里?
  • 不是作者,但人们很抱歉
  • 大多数情况下,实际上最有用的是错误消息的下一位!

标签: mysql sql inner-join


【解决方案1】:

您需要更正您的查询 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

【讨论】:

  • 查询有效,但是当我 print_r 结果时,最后一个“人”删除了其他人(我只有一个名字,“p2”的名字)
  • @CDO 这是我猜想与 php 相关的另一个问题?
  • 我在这里得到了答案stackoverflow.com/questions/23425361/…谢谢你的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多