mysql - Query get all latest records based on user_id -


i have table called photos has hundred of thousands of user uploaded photos.

each user can upload several photos.

the table schema is:

id,  user_id,  photo,  date_created 

i want latest record each user_id has posted..

i tried:

select * photos  group user_id  order date_created desc limit 300 

but bringing lot of strange results.

this seems easy query, have done hours , hours of research on stack overflow , reading many different articles on google, , can't life of me figure simple query out.

this should latest row every user:

select p.user_id, photo, date_created photos p join (     select user_id, max(date_created) max_date photos group user_id ) max_dates on p.user_id = max_dates.user_id , p.date_created = max_dates.max_date 

sample sql fiddle


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -