我想选择大多数事故中涉及哪种型号的汽车。 我使用以下查询但我得到语法错误。 请有人告诉我什么是错的..
select car.Model
from car
join car_accident_involved
on car.Car_Registration_ID = car_accident_involved.Car_Registration_ID
group by car.Model
having MAX(
select COUNT(Car_Registration_ID)
from car_accident_involved
);
答案 0 :(得分:1)
您可以在此处使用简单的子查询,例如:
select model from car
where car_registration_id =
(select car_registration_id
from car_accident_involved
group by model
order by count(car_registration_id) desc
limit 1);
答案 1 :(得分:1)
HAVING是GROUP BY的条件声明。您的查询中没有任何条件 HAVING声明,所以出现错误。 对我来说,子查询中没有必要。尝试更简单的查询,如:
SELECT c.model,COUNT(a.car_registration_id) AS Num_of_accidents FROM car c
INNER JOIN car_accident_involved a ON c.car_registration_id=a.car_registration_id
GROUP BY c.model ORDER BY Num_of_accidents DESC LIMIT 1;