我希望能够通过升序或降序对 [comment] 进行排序。我已经尝试在“order by”子句中再次执行聚合,但这没有用。我还尝试在子查询之外进行排序,这让我可以使用别名 [comment],但它只在第 0 行和第 10 行之间排序。
此查询是更大查询的较小版本。
SELECT *
FROM (SELECT ISNULL((SELECT COUNT("order")
FROM order_comment
WHERE "order" = "order"."id"
GROUP BY "order"), 0) AS [comment],
Row_number() OVER (ORDER BY "order"."id" DESC) AS [rownum]
FROM [order]
WHERE ISNULL((SELECT COUNT("order")
FROM order_comment
WHERE "order" = "order"."id"
GROUP BY "order"), 0) > 0) AS [filter]
WHERE [rownum] BETWEEN 0 AND 10
在 MySql 中如此简单!
SELECT Ifnull((SELECT COUNT(`order`)
FROM order_comment
WHERE `order` = `order`.`id`
GROUP BY `order`), 0) AS `comment`
FROM `order`
HAVING `comment` > 0
ORDER BY `comment` DESC
LIMIT 0, 10