php - How to display table data ordered by another table data row? -
i have videos , watch_later mysql tables, , want display videos data in page ordered watch_later date, current videos table looks this
id | name | views 1 | funny video | 40 2 | sad video | 20
and current watch_later table looks this
id | video_id | date 1 | 2 | 2016-02-01 2 | 1 | 2016-02-02
i've tried selecting ids watch_later table ordered date , selecting data videos table id in (previous ids), doesn't me videos data ordered watch_later date.
(if didn't make sense, want watch later system youtube.)
you have perform query join
syntax:
"select videos.*, watch_later.date videos join watch_later on videos.id=watch_later.video_id order watch_later.date desc"
if prefer sorting in ascendant order, replace desc
asc
.
with query, select either videos
fields , date
watch_later
table.
there various join
methods: if want study it, @ w3schools
edit:
i choose select fields of videos
, only watch_later.date
because of watch_later.id
field: using select *
syntax, in fact, make fields overwritten following fields same name, causing - in specific case - loss of videos.id
in result.
Comments
Post a Comment