tail Posted January 22, 2010 Share Posted January 22, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/189480-order-by-values-from-different-table/ Share on other sites More sharing options...
fenway Posted January 22, 2010 Share Posted January 22, 2010 Get the result set working first, then worry about the ordering. Quote Link to comment https://forums.phpfreaks.com/topic/189480-order-by-values-from-different-table/#findComment-1000190 Share on other sites More sharing options...
artacus Posted January 22, 2010 Share Posted January 22, 2010 Move the COUNT( rating.game_id ) / SUM( rating.rating_value ) out of the order by clause and put it in the select clause. It will still be broken but you should be able to figure out what you are doing wrong. Quote Link to comment https://forums.phpfreaks.com/topic/189480-order-by-values-from-different-table/#findComment-1000194 Share on other sites More sharing options...
tail Posted January 23, 2010 Author Share Posted January 23, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/189480-order-by-values-from-different-table/#findComment-1000286 Share on other sites More sharing options...
fenway Posted January 25, 2010 Share Posted January 25, 2010 You can use a HAVING clause to remove them entirely, or ORDER with that expression. Quote Link to comment https://forums.phpfreaks.com/topic/189480-order-by-values-from-different-table/#findComment-1001264 Share on other sites More sharing options...
tail Posted January 25, 2010 Author Share Posted January 25, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/189480-order-by-values-from-different-table/#findComment-1001517 Share on other sites More sharing options...
fenway Posted January 28, 2010 Share Posted January 28, 2010 Add those two expression as additional ORDER BY "columns". Quote Link to comment https://forums.phpfreaks.com/topic/189480-order-by-values-from-different-table/#findComment-1002784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.