0

次の mysql は、DB 内の最初の 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

author.name で並べ替えるにはどうすればよいですか? posts.title を author.name に変更すると、エラーが発生します。

Table 'comment' from one of the SELECTs cannot be used in global ORDER clause
4

2 に答える 2

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
于 2013-03-13T15:03:06.410 に答える
0

回答の更新 - 質問の読み間違い

投稿テーブルのみを使用している内部選択で注文を行っていますが、選択全体ではなく、その選択のみを注文しています。あなたが望むのは、外側の選択で author.name で注文することです。

于 2013-03-13T15:03:15.627 に答える