如何按product_id
分組之前按ap_status
的順序排列所有行'n', 'p', 'd', 'c', 'b', 'x'
?ORDER BY GROUP BY之前的指定訂單
這個查詢訂單正確,當你刪除GROUP BY
SELECT `account_id`, `product_id`, `product_name`, `ap_status`, count(`ap_id`) as `num`
FROM (accounts_products_view)
WHERE `account_id` = 13
/*GROUP BY `product_id`*/
ORDER BY FIELD(`ap_status`, 'n', 'p', 'd', 'c', 'b', 'x') desc
所以我試圖用HAVING
觀看一些類似的問題後,但是,這並非本集團之前任
SELECT account_id, product_id, product_name, ap_status, count(ap_id) as num,
CASE
WHEN ap_status = 'n' then 1
WHEN ap_status = 'p' then 2
WHEN ap_status = 'd' then 3
WHEN ap_status = 'c' then 4
WHEN ap_status = 'b' then 5
WHEN ap_status = 'x' then 6
END as `order`
FROM (accounts_products_view)
WHERE `account_id` = 13
GROUP BY product_id
HAVING `order` = MIN(`order`)
任何建議工作非常感謝
也許您可以使用您的ORDER BY查詢作爲另一個查詢的子查詢,該查詢對其結果使用GROUP BY。或者使用臨時表或某物。 – rocky3000
向我們顯示您的查詢。無論如何,如果你要分組,並應用一個聚合函數,你不需要訂單 –