SELECT
airport_id "Airport id",
crew_type "Crew type",
min(crew_age) "Minimum age",
Round(avg(crew_age),0) "Average age",
max(crew_age) "Maximum age"
FROM crew
GROUP by crew_type
问题在于第二部分。第二部分是我需要根据airport_id字段获取机场名称。我这样做:
SELECT
crew.airport_id,
crew_type "Crew type",
min(crew_age) "Minimum age",
Round(avg(crew_age),0) "Average age",
max(crew_age) "Maximum age",
airport.airport_name
FROM crew, airport
WHERE
crew.airport_id = airport.airport_id
GROUP by crew.crew_type
虽然有效,但airport_id与airport_name不匹配。 Airport_id和airport_name字段都相同。
答案 0 :(得分:0)
分组时,所有字段必须是group by的一部分,或者必须与max,avg等聚合函数一起使用。
选择
crew.airport_id,
crew_type“船员类型”,
min(crew_age)“最低年龄”,
回合(平均(crew_age),0)“平均年龄”,
max(crew_age)“最大年龄”,
airport.airport_name
来自船员,机场 哪里 crew.airport_id = airport.airport_id GROUP by crew.airport_id,crew.crew_type,airport.airport_name