Okay, so I got 2 tables. One holds the recipes, and the second all the ratings.
recipes_ratings:
id, rating, user_id, recipe_id
recipes:
id, name
I would like to count all the rows for each recipe. Then sort and show the recipes that are most rated and descending.
What is the simplest way to do? I have thought to make a query
SELECT * FROM recipes_ratings
And then count with a loop through the ratings, and store into array, and then sort out from that.
But can it be done simpler? One query only to do it all..or?
Depending on how you want to calculate the overall rating per recipe, you might need to adapt the calculation. But the query should be something like this:
Beware, that this will also output recipes without any rating. If you don’t want to have them returned, change
LEFT JOINtoJOIN.