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
Old 04-24-2005, 09:22 AM MySQL: Adding Values
Bloodsyne's Avatar
Skilled Talker

Posts: 52
Trades: 0
How would I go about taking all of the values in a table and adding them together?

(as in, there are 15 entries, how would I add them all up so that I can divide them and get the mean?)
Bloodsyne is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-24-2005, 10:25 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
if you want the average of all entries, use SQL like this:

select avg(col_name)
from table
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 04-24-2005, 04:55 PM
Bloodsyne's Avatar
Skilled Talker

Posts: 52
Trades: 0
I tired several variations of that and none worked.

How else can I go about this?
Bloodsyne is offline
Reply With Quote
View Public Profile
 
Old 04-24-2005, 05:01 PM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
Please post your code, using the avg() function is the easiest way to do it.
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 04-24-2005, 05:33 PM
Bloodsyne's Avatar
Skilled Talker

Posts: 52
Trades: 0
PHP Code:
function get_rating($id) {
    global 
$host,$user,$pass,$db;
    
    
$link mysql_connect($host,$user,$pass);
    
mysql_select_db($db);
    
    
$query_c " SELECT avg(`rating`),`n_id` FROM `news_rating` WHERE n_id = $id ";
    
$result_c mysql_query($query_c);
    
    
$num_rows mysql_num_rows($result_c);
    if(
$num_rows == "0") {
        
$rating "N/A";
    } else {    
        while(
$row mysql_fetch_array($result_cMYSQL_ASSOC)) {
            
$rating $row['rating'];
        }
    }
    
    
mysql_free_result($result_c);
    
mysql_close($link);
        
    return 
$rating;

I could be using it wrong... I don't know.
Bloodsyne is offline
Reply With Quote
View Public Profile
 
Old 04-24-2005, 05:39 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
use an alias for the field name

select avg('rating') as rating_alias from ...
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-24-2005, 05:47 PM
Bloodsyne's Avatar
Skilled Talker

Posts: 52
Trades: 0
Alright, I got it to work.

Now, this is going to sound newbish, but I can't remember (for the life of me) how to make a string display only the first 3 characters (there are 5 in the average). How do I do this again?
Bloodsyne is offline
Reply With Quote
View Public Profile
 
Old 04-24-2005, 07:16 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
substr()
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to MySQL: Adding Values
 

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.35032 seconds with 12 queries