MySQL Many for Many table: getting number of results -
i have database of 3 tables: users
photos
likes
, basic outline so:
users: user_id name photos: photo_id title url user_id likes: user_id photo_id
what want enable user many photos , have photos liked many users.
obviously, want order these photos number of likes, can join
, count()
what need can't figure out return number of 'likes' each photo has. how this?
my current sql is:
select photos.photo_id, photos.title, photos.url, users.name photos left outer join users on users.user_id = photos.user_id left outer join likes on likes.photo_id = photos.photo_id group likes.photo_id order count(*) desc limit 20
just add count(*)
select list:
select photos.photo_id, photos.title, photos.url, users.name, count(*) photos left outer join users on users.user_id = photos.user_id left outer join likes on likes.photo_id = photos.photo_id group likes.photo_id order count(*) desc limit 20
Comments
Post a Comment