sql - Multiple ORDER BY (get latest element) MySQL -
i have following tables : radios, podcasts , shows. radio has many podcasts, , podcasts has many shows. each podcast can ordered history, , each show can ordered publication_date.
i want podcasts associated latest show.
the query looks :
select r.name radio_name,pod.*,sh.* podcasts pod inner join radios r on (r.id=pod.radio_id) inner join shows sh on (sh.podcast_id=pod.id) order pod.history limit 5
i'd have second order sh.publication_date
don't know should be.
if want latest show each pod cast, need information. here 1 method:
select r.name radio_name,pod.*,sh.* podcasts pod inner join radios r on r.id = pod.radio_id inner join shows sh on sh.podcast_id = pod.id not exists (select 1 shows sh2 sh2.podcast_id = sh.podcast_id.id , sh2.publication_date > sh.publication_date ) order sh.publication_date desc limit 5;
in addition "obvious" indexes on join columns, walso want index on shows(podcast_id, publication_date)
.
i guessing want results ordered recent show publication date.
Comments
Post a Comment