moon 111 Posted March 23, 2008 Share Posted March 23, 2008 I have two relevent tables: sites and rating. Rating has these rows: sid (site id), uid (userid) and rating. When someone votes on a website it adds to rating the sid of the site their uid and their rating. What I need to do is sort the site table by rating. How can I do that? Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/ Share on other sites More sharing options...
bobinindia Posted March 23, 2008 Share Posted March 23, 2008 ORDER BY rating; ASC or DESC as you need. If I understood the question Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498723 Share on other sites More sharing options...
moon 111 Posted March 23, 2008 Author Share Posted March 23, 2008 No, I need to order the table 'site' by the row 'rating' in the table 'rating' Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498751 Share on other sites More sharing options...
moon 111 Posted March 23, 2008 Author Share Posted March 23, 2008 I kind've need the help quickly.... Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498760 Share on other sites More sharing options...
wildteen88 Posted March 23, 2008 Share Posted March 23, 2008 I kind've need the help quickly.... Umm, you're getting free help from a public community, which members donate their free time to help. Also in order to get you a faster reply would help if you posted your current query and posting your question within the correct board. PHP help is for PHP. Yours is related to MySQL (I presume) so I'll move this tread. Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498767 Share on other sites More sharing options...
mwasif Posted March 23, 2008 Share Posted March 23, 2008 No, I need to order the table 'site' by the row 'rating' in the table 'rating' Please explain this with an example. Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498773 Share on other sites More sharing options...
moon 111 Posted March 23, 2008 Author Share Posted March 23, 2008 I have a table sites that has the cols id, title, description, uid. I also have a table rating with the rows sid, uid, rating. sid reffers to the site. Now I want to sort 'sites' by the average of all of the ratings reffering to that site. This is a want I would do if I could (obviously not legal: SELECT * FROM sites ORDER BY AVG(SELECT rating FROM rating WHERE sid = '".$sid."') DESC . Clear? Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498781 Share on other sites More sharing options...
moon 111 Posted March 23, 2008 Author Share Posted March 23, 2008 Maybe a JOIN? Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498787 Share on other sites More sharing options...
moon 111 Posted March 23, 2008 Author Share Posted March 23, 2008 Would this work: SELECT * FROM site, rating WHERE site.id = rating.sid ORDER BY AVG(rating) DESC Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498799 Share on other sites More sharing options...
wildteen88 Posted March 23, 2008 Share Posted March 23, 2008 you cannot use a table name within the ORDER BY clause it has to be a fieldname. So to order by a field from the results table you'd results.field_name Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498812 Share on other sites More sharing options...
moon 111 Posted March 23, 2008 Author Share Posted March 23, 2008 This doen't work either: SELECT * FROM site, rating WHERE site.id = rating.sid ORDER BY AVG(rating.rating) DESC And yes I understood. There is a field rating in the table rating. Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498824 Share on other sites More sharing options...
wildteen88 Posted March 23, 2008 Share Posted March 23, 2008 You might want to remove the AVG() function. SELECT * FROM site, rating WHERE site.id = rating.sid ORDER BY rating.rating DESC Link to comment https://forums.phpfreaks.com/topic/97467-sorting-help/#findComment-498826 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.