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? Quote Link to comment 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 Quote Link to comment 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' Quote Link to comment 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.... Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment Share on other sites More sharing options...
moon 111 Posted March 23, 2008 Author Share Posted March 23, 2008 Maybe a JOIN? Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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.