我正在使用以下SQL Query:
SELECT `comment`.`id` AS `comment_id` , count( `comment_likes`.`comment_id` ) AS `number_of_likes`
FROM `comment`
LEFT JOIN `comment_likes` ON `comment`.`id` = `comment_likes`.`comment_id`
WHERE `comment`.`id`
IN ( 10, 5, 7, 8, 3, 2, 9 )
GROUP BY `comment`.`id`
此处查询的结果如下:
comment_id number_of_likes
2 0
3 1
5 0
7 0
8 0
9 0
10 0
我不想要......?我想要条件相同的顺序,即 WHERE comment
。id
IN(10,5,7,8,3,2,9)。
所以我希望结果如下:
comment_id number_of_likes
10 0
5 0
7 0
8 0
3 1
2 0
9 0
任何人都可以帮助我......?
先谢谢.....
答案 0 :(得分:1)
添加显式ORDER BY
。一个有用的功能是FIELD()
:
ORDER BY FIELD(comment.id, 10, 5, 7, 8, 3, 2, 9)
答案 1 :(得分:1)
您需要使用MySQL的FIELD()
才能获得所需的结果
SELECT `comment`.`id` AS `comment_id` ,
count( `comment_likes`.`comment_id` ) AS `number_of_likes`
FROM `comment` LEFT JOIN `comment_likes` ON
`comment`.`id` = `comment_likes`.`comment_id`
WHERE `comment`.`id`
IN ( 10, 5, 7, 8, 3, 2, 9 )
GROUP BY `comment`.`id`
ORDER BY FIELD(comment.id, 10, 5, 7, 8, 3, 2, 9)