|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
Hi all,
I know that this is a real n00b question, and I'm not really a n00b anymore, but I've never needed something like this before... I have a list of numbers in a db, and I want to write a function which calculates the average of it. You can guess that it's not the same amount of numbers all the time. It's an average rating of a pic users can vote at. Does anyone know how to calculate this? It would be great. Cheers,
__________________
Work to live, don't live to work |
|
#2
|
||||
|
||||
|
Check out the following:
Code:
mysql> describe math; +-------+---------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------------+------+-----+---------+----------------+ | id | int(10) unsigned zerofill | | PRI | NULL | auto_increment | | num | int(10) | YES | | NULL | | +-------+---------------------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql> select * from math; +------------+------+ | id | num | +------------+------+ | 0000000001 | 5 | | 0000000002 | 5 | | 0000000003 | 5 | | 0000000004 | 5 | | 0000000005 | 5 | | 0000000006 | 7 | | 0000000007 | 7 | | 0000000008 | 7 | | 0000000009 | 7 | | 0000000010 | 7 | +------------+------+ 10 rows in set (0.00 sec) mysql> select count(*) as cnt, SUM(num) as sum, (SUM(num)/count(*)) as avg FROM math; +-----+------+------+ | cnt | sum | avg | +-----+------+------+ | 10 | 60 | 6.00 | +-----+------+------+ 1 row in set (0.00 sec)
__________________
Please don't PM me asking for solutions outside the scope of a thread. Keeping all responses in a thread stands to help others who come along later, which is after all what this forum's all about. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Calculate average in PHP |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|