Jump to content

Recommended Posts

Sort result by a different tables values

I'm working on a script that shows games compiled in a MySQL database. I'm trying to sort them by highest rated.

 

The ratings are stored in a different table. The tables are set up as follows:

 

table `info`

| id | user_id | name | desc | category | plays | date_added |

 

table `rating`

| game_id | user_id | rating_value |

 

I think this is possible using a JOIN. I don't know much about JOIN's but it seems that it could be done. The way I'm trying to do it right now is add up the rating values for the specific ID, then divide that by the number of ratings for the specific ID, then sort the result by the highest number.

 

I'm using this query to no avail:

SELECT `id` , `name`

FROM `info`

JOIN `rating` AS `rating`

ORDER BY (

COUNT( rating.game_id ) / SUM( rating.rating_value )

) DESC

 

Any help is much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/189480-order-by-values-from-different-table/
Share on other sites

Got it!

 

SELECT `id` , `name`

FROM `info`

LEFT JOIN `rating` ON rating.game_id = info.id

GROUP BY info.id

ORDER BY (

AVG( rating.rating_value )

) DESC

 

Thanks for the help! Is there a way I can make it so results with < 10 votes doesn't come up first?

I got to this point:

SELECT info.id 
     , info.name
  FROM info
LEFT OUTER
  JOIN rating 
    ON rating.game_id = info.id
GROUP 
    BY info.id
HAVING COUNT(rating.rating_value) >= 10
ORDER
    BY AVG(rating.rating_value) DESC

 

However only games with > 10 votes showed in the result. How can I make it so all of the games show up, but order priority goes to games with > 10 votes and an avg rating higher than 3?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.