グループ化する前に、すべての行ap_status
をこの順序で並べ替えるにはどうすればよいですか?'n', 'p', 'd', 'c', 'b', 'x'
product_id
このクエリは、を削除すると正しく順序付けられます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`)
任意の提案は大歓迎です