在分组之前,我如何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`)
非常感谢任何建议