MySQL统计一个字段的多个值对应的数据

SELECT
	ugi.goods_id,
	SUM(case when smgs.`status` = 0 then 1 else 0 end) rent_count,
	SUM(case when smgs.`status` = 1 then 1 else 0 end) renting_count,
	SUM(case when smgs.`status` = 2 then 1 else 0 end) lost_count
FROM
	goods_stock AS smgs
LEFT JOIN goods_info AS ugi ON smgs.unique_id = ugi.unique_id
GROUP BY
ugi.goods_id


goos_stock表的status字段有0:待用,1:借出,2:丢失,分别统计这仨状态对应的数量

 

你可能感兴趣的:(MySQL统计一个字段的多个值对应的数据)