mysql multi count with WHERE
i have the following query i made:
SELECT COUNT(*) AS item_count,
reseller_id, count(**WHERE sold=1**) as sold_count
count(**WHERE refunded=1**) 开发者_Python百科as rrefunded
FROM store GROUP BY
reseller_id
sold is either 1 or 0 and same for refund.
I want to check each reseller how much they sold and how much refunded, but I don't have a clue on how to do it in one query.
if its 1 or 0 you can do SUM()
SELECT SUM(sold) as sold_count, SUM(refunded) as refund_count FROM store
Try this instead:
SELECT
reseller_id,
COUNT(*) AS item_count,
sum(case sold when 1 then 1 else 0 end) as sold_count,
sum(case refunded when 1 then 1 else 0 end) as refunded_count
FROM store
GROUP by reseller_id
精彩评论