Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
calculate average from rows in a database
Old 06-09-2008, 04:49 AM calculate average from rows in a database
abhi's Avatar
Super Talker

Posts: 100
Name: abhishek
Location: mauritius
Trades: 0
hi, can anyone tell me how to calculate the average of data retrieves from a database. The situation is I have a table "number" which consist of several data like "2,5,4,8,6...." I want to calculate the average and display it on the browser. thanks in advance for your help.
< removed >

Last edited by chrishirst; 06-11-2008 at 07:05 AM..
abhi is offline
Reply With Quote
View Public Profile Visit abhi's homepage!
 
 
Register now for full access!
Old 06-09-2008, 06:42 AM Re: calculate average from rows in a database
Average Talker

Posts: 18
Name: Phil
Location: Scotland
Trades: 0
It's very simple, when selecting data from a table with a query, such as:
SELECT ages FROM buildings WHERE town = 'london'

If you wanted the average of all those ages you do:
SELECT AVG(ages) FROM buildings WHERE town = 'london'

Note that this will return one row, I think. Not one for every building in London that the database stores >_< like the first query would. I am not sure what happens if you mix an average in with a normal one.
Bozebo is offline
Reply With Quote
View Public Profile Visit Bozebo's homepage!
 
Old 06-10-2008, 10:35 AM Re: calculate average from rows in a database
Skilled Talker

Posts: 62
Name: Tom Wright
Location: Brighton, UK
Trades: 0
Quote:
Originally Posted by Bozebo View Post
Note that this will return one row, I think. Not one for every building in London that the database stores >_< like the first query would. I am not sure what happens if you mix an average in with a normal one.
Although I don't think the OP wanted to know this, it is possible to mix composite queries (e.g. average or count) with 'normal' queries in a couple of ways:
  • Joins (left, right, etc...) via GROUP BY
  • Subqueries, where the result of one query is passed to the parent query
SQL optimization can be really interesting. If you really want to know more though you might want to try the MySQL (or MSSQL, though I've never been) support forums - they're full of interesting snibbets like these.
Regards,
Tom
__________________
My site:
Please login or register to view this content. Registration is FREE
tomythius is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to calculate average from rows in a database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26201 seconds with 12 queries