hnumar7 Posted May 11, 2008 Share Posted May 11, 2008 I hope I can explain this well enough for someone to offer me some help. I have a table called rate which has values 1-5 and an ID the table looks like this: rated - ID 5 10 4 10 3 8 2 8 I am attempting to try and get the highest average of any ID based on the values in column rated so in the above example ID 10 would have an average of 4.5 and ID 8 would have an average of 2.5 , so ID 10 would be have the highest average and ID 8 would have the lowest average. Does anyone know what my select statement or php code should look like to accomplish this? I am somewhat comfortable with PHP. Any help or suggestions would be very much appreciated Thanks Henry Quote Link to comment Share on other sites More sharing options...
mezise Posted May 12, 2008 Share Posted May 12, 2008 If I get you correctly, to get ID with highest AVG use this query: SELECT ID , AVG(rated) avgRate FROM rate GROUP BY ID ORDER BY avgRate DESC LIMIT 1 ; To get the lowest, change "avgRate DESC" to "avgRate ASC". Quote Link to comment Share on other sites More sharing options...
hnumar7 Posted May 12, 2008 Author Share Posted May 12, 2008 If I get you correctly, to get ID with highest AVG use this query: SELECT ID , AVG(rated) avgRate FROM rate GROUP BY ID ORDER BY avgRate DESC LIMIT 1 ; To get the lowest, change "avgRate DESC" to "avgRate ASC". Thanks so much.... I knew this was easy just couldn't figure it out!!... Thanks again for helping out a Newbie 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.