gpt4 book ai didi

mysql - 重写查询以防止 only_full_group_by 错误

转载 作者:行者123 更新时间:2023-11-29 11:32:19 28 4
gpt4 key购买 nike

我有以下查询:

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 设置 已激活。此查询不再适用于这种形式:

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

关于mysql - 重写查询以防止 only_full_group_by 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37207359/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com