Bopo Posted March 30, 2009 Share Posted March 30, 2009 Hi I just want to make sure this is possible, as there's a few steps I will need to complete before it, basically here's what I want to do Use a select * query to find all records with a ID number equal to the one I'm querying for Within each record, there will be a column with a number of 1 to 5, I would loop through each record and add them all together to make a total Next, the amount of records returned will be counted, and this number will be divided with the total above Them the average is displayed on the page So is this plausible? Link to comment https://forums.phpfreaks.com/topic/151681-record-retrival-question/ Share on other sites More sharing options...
Maq Posted March 30, 2009 Share Posted March 30, 2009 Sure it is. You can use the aggregate mysql function, AVG(). Does each record have 5 numbers in it or you just want to average everything(*)? Link to comment https://forums.phpfreaks.com/topic/151681-record-retrival-question/#findComment-796535 Share on other sites More sharing options...
Bopo Posted March 30, 2009 Author Share Posted March 30, 2009 Thanks for the reply, that function looks really useful Each record will only have 1 number, which will range from 1-5, like ID | Rating 1 3 1 5 1 5 1 2 1 4 1 5 1 3 1 2 1 4 1 4 Then loop through and add all the rating's up = 37 Divide by the amount of records, 10, therefore 37 / 10 = 3.7 avg The only thing I'm going to struggle with is the looping through all that rating records and adding them all together, If someone could link to or provide a short example, I'd appreciate it . Link to comment https://forums.phpfreaks.com/topic/151681-record-retrival-question/#findComment-796545 Share on other sites More sharing options...
corbin Posted March 30, 2009 Share Posted March 30, 2009 With AVG, MySQL does the looping for you. Example: SELECT AVG(rating) as avg_rating WHERE id = 1; Link to comment https://forums.phpfreaks.com/topic/151681-record-retrival-question/#findComment-796548 Share on other sites More sharing options...
Bopo Posted March 30, 2009 Author Share Posted March 30, 2009 I can't try it atm, but that's looks great, thanks for posting it Link to comment https://forums.phpfreaks.com/topic/151681-record-retrival-question/#findComment-796553 Share on other sites More sharing options...
corbin Posted March 30, 2009 Share Posted March 30, 2009 Technically Maq beat me to it by an hour, but..... lol ;p Link to comment https://forums.phpfreaks.com/topic/151681-record-retrival-question/#findComment-796554 Share on other sites More sharing options...
Maq Posted March 30, 2009 Share Posted March 30, 2009 Technically Maq beat me to it by an hour, but..... lol ;p Hehe @Bopo, if corbin and I understood your question correctly, then AVG() is the perfect solution for you. Link to comment https://forums.phpfreaks.com/topic/151681-record-retrival-question/#findComment-796556 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.