Adding values from different columns in

wxdqz

New Member
Could anyone out there help me with this annoying problem I am having, It would be much appreciated.

Ive been working on a php script for a while and I can't figure out how to get it working.

I have a mysql table Count defined as

Resource (PK)
CountJan
CountFeb
.
.
.
.
.
CountDec

Where each of the Countxxx columns are smallint(5).

This script takes in two variables $startmonth and $endmonth which each relate to a column for one of the months of the year(eg CountJan).

I would like to make a select statement which adds the values of the columns in between together, and finds the top 10 values (Resources) out of them.

For instance if I did a select statement that had $startmonth as CountJan and $endmonth as CountJun then it would add up all the values in CountJan all the way to CountJun, and find the top 10 values and be able to display them.

So far I have only been able to work out the query if there was a total count column, but I have to have a changing total count column depending on which months are chosen for the start and finish.

SELECT Resource, CountTotal
FROM Count
ORDER BY CountTotal
LIMIT 0,10;

The final result would hopefully be something like:

Resource Count
10 234
23 200
41 198
2 194
61 163
95 120
168 119
.... ....

----------------------

Thanks

Jonathan Batten
 
Back
Top