php - Get LAST in MySQL GROUP By query -
i have table of flights , struggling identify last city each flight# landed at.
the incorrect query is:
select distinct `flight_nmbr`, count(record_id) flights, min(`depart_date_time`) `first_flight`, max(`depart_date_time`) `last_flight`, max(`location`) `current_location` history group `flight_nmbr` having records > 1 order max(`depart_date_time`) desc;
this statement indicated last flight (aircraft located) winston-salem, or zanesville (which max city name).
i need last location (the location max(depart_date_time)), last / recent flight, atlanta , houston.
microsoft access has last function query, migrating away access, using mysql , php.
can tweak query?
many thanks!
you can use substring group_concat trick
select distinct flight_nmbr, count(record_id) flights, min(depart_date_time) first_flight, max(depart_date_time) last_flight, substring_index(group_concat(location order depart_date_time desc),',',1) current_location history group flight_nmbr having records > 1 order max(depart_date_time) desc;
Comments
Post a Comment