我在计算特定地点的乘客和行李总数时遇到问题。
这是我的表enter image description here
但我的query
显示总乘客总是2,
最后,这是query
select
ticket_placeto,
(select count(*) as count from tickets where passenger_type <> 'Baggage' group by ticket_placeto)as Pass ,
ifnull((select count(*) as count from tickets where passenger_type = 'Baggage' group by ticket_placeto),0) as Baggage
from tickets
答案 0 :(得分:1)
将您的查询改为使用条件聚合:
SELECT
ticket_placeto,
COUNT(CASE WHEN passenger_type <> 'Baggage' THEN 1 END) AS Pass,
COUNT(CASE WHEN passenger_type = 'Baggage' THEN 1 END) AS Baggage
FROM tickets
GROUP BY ticket_placeto;
这里的想法是,对于每个ticket_placeto
组,您希望根据乘客类型采取两个条件计数。