我有一些问题如何从1个表中查询具有不同条件的多个计数。
SELECT COUNT(*) FROM account_holder
INNER JOIN vehicle
ON account_holder.`id`= vehicle.`accholder_id`
WHERE vehicle.`class_code`= 'BM' AND (vehicle.`roadtax_expired` > CURDATE())
AND (vehicle.`block_flag`='N') AND (vehicle.`is_blacklist`='N')
场景:我想用不同的代码计算表格车辆中的class_code。例如,我的查询只计算BM的代码,但是如何在一个查询中计算其他class_code? p>
其他不同的class_code:BL,CA,CE ......
答案 0 :(得分:2)
您需要GROUP BY
子句来计算每个class_code
。
SELECT vehicle.`class_code`, COUNT(*)
FROM account_holder
INNER JOIN vehicle
ON account_holder.`id`= vehicle.`accholder_id`
WHERE (vehicle.`roadtax_expired` > CURDATE())
AND (vehicle.`block_flag`='N')
AND (vehicle.`is_blacklist`='N')
GROUP BY vehicle.`class_code`
答案 1 :(得分:1)
您可以尝试使用group by,
SELECT COUNT(vehicle.`class_code`) as class_code count FROM account_holder
INNER JOIN vehicle
ON account_holder.`id`= vehicle.`accholder_id`
WHERE (vehicle.`roadtax_expired` > CURDATE())
AND (vehicle.`block_flag`='N') AND (vehicle.`is_blacklist`='N') group by vehicle.`class_code`