select *, count(*) as total
from customer_data
where category = 'Fashion'
group by customer_id
order by total desc
从这个查询结果我怎么能拿到总数> gt的字段? 5?
答案 0 :(得分:1)
您需要HAVING
条款。另请尽量不要选择不在GROUP BY
子句中的非聚合列或列。
SELECT aa.*, _aa.total
FROM (
SELECT customer_id, COUNT(*) AS total
FROM customer_data
WHERE category = 'Fashion'
GROUP BY customer_id
HAVING COUNT(*) > 5
) AS _aa
INNER JOIN customer_data AS aa
ON _aa.customer_id = aa.customer_id
ORDER BY _aa.total DESC