SELECT * FROM
(
SELECT messages.from AS `from`, messages.creation AS `creation`, account_images.profile AS `profile`, bio.user_full_name AS `user_full_name`
FROM messages
INNER JOIN account_images ON account_images.uuid=messages.from
INNER JOIN bio ON bio.uuid=messages.from
WHERE messages.to='{$user}'
ORDER BY `creation` DESC
)
GROUP BY `from`
为什么这个查询不起作用?嵌套的select语句有问题吗?请帮我解决这个问题...
答案 0 :(得分:0)
FROM
是一个mysql保留关键字,使用from作为列名,应该在backtics中,否则你会收到错误messages
。from
SELECT * FROM
(
SELECT messages.`from` AS `from`, messages.creation AS `creation`, account_images.profile AS `profile`, bio.user_full_name AS `user_full_name`
FROM messages
INNER JOIN account_images ON account_images.uuid=messages.`from`
INNER JOIN bio ON bio.uuid=messages.`from`
WHERE messages.to='{$user}'
ORDER BY `creation` DESC
)
GROUP BY `from`
答案 1 :(得分:0)
除了不带引号的from
问题之外,连接的顺序错误。在定义on
子句之前,您不能引用表别名,并且您需要在子查询上使用表别名:
SELECT *
FROM (SELECT messages.from AS `from`, messages.creation AS `creation`,
account_images.profile AS `profile`, bio.user_full_name AS `user_full_name`
FROM account_images INNER JOIN
bio
ON bio.uuid = messages.`from` INNER JOIN
messages
ON account_images.uuid = messages.`from`
WHERE messages.to='{$user}'
ORDER BY `creation` DESC
) t
GROUP BY `from`;
最后,您正尝试使用明确描述为group by
扩展的MySQL文档中不可靠的技术来获取组的最后一条消息(请参阅here)。