Reply
database addition
Old 04-11-2007, 02:03 PM database addition
FutileSoul's Avatar
Ultra Talker

Posts: 381
Location: Michigan
how would i go about taking two or more fields in a database table and adding them up?


example table - I want to add up scores and display the total in the total column

| NAME | SCORE 1 | SCORE 2 | SCORE 3 | TOTAL |
-------------------------------------------------
|--Bill--|---50----|---20----|----70----|---??---|
|-Dave-|---60----|---10----|----20----|---??---|
|-Josh--|---80----|---50----|----10----|---??---|
__________________
Mangregation - The Congregation of Man

Last edited by FutileSoul : 04-11-2007 at 02:05 PM.
FutileSoul is offline
Reply With Quote
View Public Profile Visit FutileSoul's homepage!
 
When You Register, These Ads Go Away!
Old 04-11-2007, 04:26 PM Re: database addition
chrishirst's Avatar
Super Moderator

Posts: 13,609
Location: Blackpool. UK
you could just add them together of course

select fieldlist,field1 + field2 +fieldn as total from table
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-11-2007, 04:39 PM Re: database addition
FutileSoul's Avatar
Ultra Talker

Posts: 381
Location: Michigan
so i wouldn't need a column named "total" in my database
__________________
Mangregation - The Congregation of Man
FutileSoul is offline
Reply With Quote
View Public Profile Visit FutileSoul's homepage!
 
Old 04-11-2007, 04:55 PM Re: database addition
chrishirst's Avatar
Super Moderator

Posts: 13,609
Location: Blackpool. UK
Not for the addition you wouldn't
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-12-2007, 03:21 AM Re: database addition
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,024
Name: Forrest Croce
Location: Seattle, WA
Databases are about a lot more than just storing data. Queries let you look at your data in new ways, and see things that you aren't storing directly. Averages, totals, top 10 lists, and so on.

You might want to create a "view" ( a query saved into the database ) with the formula for "total" though. If you decide to add a Score 4, Score 5, and so on, you could make the change in just one place if the rest of your site / app queries the view instead of the underlying table.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 04-12-2007, 08:16 AM Re: database addition
FutileSoul's Avatar
Ultra Talker

Posts: 381
Location: Michigan
Quote:
Originally Posted by ForrestCroce View Post
Databases are about a lot more than just storing data. Queries let you look at your data in new ways, and see things that you aren't storing directly. Averages, totals, top 10 lists, and so on.

You might want to create a "view" ( a query saved into the database ) with the formula for "total" though. If you decide to add a Score 4, Score 5, and so on, you could make the change in just one place if the rest of your site / app queries the view instead of the underlying table.
this sounds like what i need, i will try to figure it out
from what i see if i add the scores up in the code itself i wouldn't be able to put the total scores in descending order, or would I??
__________________
Mangregation - The Congregation of Man
FutileSoul is offline
Reply With Quote
View Public Profile Visit FutileSoul's homepage!
 
Old 04-12-2007, 08:28 AM Re: database addition
chrishirst's Avatar
Super Moderator

Posts: 13,609
Location: Blackpool. UK
You can order by an column alias ( column AS alias_name ) in exactly the same way as you can using a column name.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-12-2007, 09:04 AM Re: database addition
FutileSoul's Avatar
Ultra Talker

Posts: 381
Location: Michigan
i figured out where to put the formulas, but can someone point me in the right direction as to what to put for the formulas, how the format of the formulas go
__________________
Mangregation - The Congregation of Man
FutileSoul is offline
Reply With Quote
View Public Profile Visit FutileSoul's homepage!
 
Old 04-12-2007, 01:09 PM Re: database addition
Skilled Talker

Posts: 94
Depending on how often how many records etc. It may be best for performance to actually create a row total.
__________________
Sell Templates? Try our Template Monster WebAPI absolutely free! See a live Template Shop Demo
ChadR is offline
Reply With Quote
View Public Profile
 
Old 04-13-2007, 02:04 AM Re: database addition
chrishirst's Avatar
Super Moderator

Posts: 13,609
Location: Blackpool. UK
Quote:
Originally Posted by FutileSoul View Post
i figured out where to put the formulas, but can someone point me in the right direction as to what to put for the formulas, how the format of the formulas go
Which formulas have you worked out ?

A simple addition is as I put in post #2
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to database addition
 

Thread Tools

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

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