Need some help with putting this query together. I’m using Mysql
I have two tables
Video – contains videos uploaded by users
- video_id
- user_id
- category_id
Vote – contains a vote given by any user for a particular video
- vote_id
- video_id
- user_id
I don’t want to hardcode the categories in the query – the Categories are stored in the Category table which has category_id and category_name
I basically want a query that pulls the top 3 videos (ones with max votes) for each category.
Sample Data – Video Table
video_id | user_id | category_id 1 100 10 2 101 10 3 102 11 4 103 11 5 104 11 6 105 11 7 105 12
Sample Data – Vote Table
vote_id | video_id | user_id 11 3 105 12 3 102 13 3 111 14 3 121 15 4 200 16 4 201 17 1 222
Sample Data – Category Table
category_id | category_name 10 HipHop 11 Rap 12 Country
This is the type of problem that is trivial to solve with ranking functions. However, since MySQL does not yet support them, it makes it more difficult. In this design, I assumed that
video_idwas the primary key of the Video table.