重写查询以防止 only_full_group_by 错误
Rewrite query to prevent only_full_group_by error
我有以下查询:
SELECT f.flight_number, f.flight_date, f.airport, a.iata, a.iso3166 AS airport_country_code, air.icao
FROM flights_database f
LEFT JOIN airlines air ON air.name = f.airline
JOIN airports a ON f.airport = a.airportNameClean
WHERE f.flight_date
BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND DATE_ADD(CURDATE() , INTERVAL 1 DAY)
GROUP by f.flight_date, f.airport, f.flight_number, a.iata
ORDER by f.airport, f.flight_number
现在我已经升级了 MysQL 并激活了 only_full_group_by setting
。
此查询不再适用于以下形式:
Expression #6 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'air.ICAO' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
当然我可以禁用此设置,但我确实知道引入此设置是有原因的。简单地将 icao
添加到 GROUP BY
子句会引入不同的结果。
如何重写此查询以在启用此设置的情况下实现相同的行为?
你不能简单地做 max(icao)
吗?
SELECT f.flight_number, f.flight_date, f.airport, a.iata, a.iso3166 AS airport_country_code, max(air.icao)
FROM flights_database f
LEFT JOIN airlines air ON air.name = f.airline
JOIN airports a ON f.airport = a.airportNameClean
WHERE f.flight_date
BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND DATE_ADD(CURDATE() , INTERVAL 1 DAY)
GROUP by f.flight_date, f.airport, f.flight_number, a.iata
ORDER by f.airport, f.flight_number
我有以下查询:
SELECT f.flight_number, f.flight_date, f.airport, a.iata, a.iso3166 AS airport_country_code, air.icao
FROM flights_database f
LEFT JOIN airlines air ON air.name = f.airline
JOIN airports a ON f.airport = a.airportNameClean
WHERE f.flight_date
BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND DATE_ADD(CURDATE() , INTERVAL 1 DAY)
GROUP by f.flight_date, f.airport, f.flight_number, a.iata
ORDER by f.airport, f.flight_number
现在我已经升级了 MysQL 并激活了 only_full_group_by setting
。
此查询不再适用于以下形式:
Expression #6 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'air.ICAO' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
当然我可以禁用此设置,但我确实知道引入此设置是有原因的。简单地将 icao
添加到 GROUP BY
子句会引入不同的结果。
如何重写此查询以在启用此设置的情况下实现相同的行为?
你不能简单地做 max(icao)
吗?
SELECT f.flight_number, f.flight_date, f.airport, a.iata, a.iso3166 AS airport_country_code, max(air.icao)
FROM flights_database f
LEFT JOIN airlines air ON air.name = f.airline
JOIN airports a ON f.airport = a.airportNameClean
WHERE f.flight_date
BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND DATE_ADD(CURDATE() , INTERVAL 1 DAY)
GROUP by f.flight_date, f.airport, f.flight_number, a.iata
ORDER by f.airport, f.flight_number