以下mysql将返回我的数据库中的前10个帖子,以及与这10个帖子相关联的评论,以及与帖子相关联的用户,整个事件按帖子的标题排序。
SELECT * FROM
(SELECT * FROM posts LIMIT 0,10 ORDER BY posts.title) as post
LEFT JOIN comments AS comment ON comment.postId = post.id,
authors AS author
WHERE post.authorId = author.id
如何按作者姓名排序?将posts.title更改为author.name会给我一个错误:
Table 'comment' from one of the SELECTs cannot be used in global ORDER clause
答案 0 :(得分:0)
您需要在子查询中切换ORDER BY和LIMIT。但是,您还希望将ORDER BY移动到主查询,而不是子查询。所以你想要:
SELECT * FROM (SELECT * FROM posts LIMIT 0,10) as post
LEFT JOIN comments as comment on comment.postId = post.id, authors as author
WHERE post.authorId = author.id ORDER BY author.name, posts.title
答案 1 :(得分:0)
更新答案 - 误读问题
你正在一个内部选择上做一个订单,它只使用posts表,它只命令选择,而不是整个选择。你想要的是由外部选择上的author.name订购。