簡體   English   中英

具有With子查詢MySql

[英]Having With Sub query MySql

我想選擇大多數事故涉及哪種模型車。 我正在使用以下查詢,但是出現語法錯誤。 請有人告訴我怎么了..

    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
    );

您可以在此處使用簡單的子查詢,例如:

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);

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM