Hi,
I'm using Mysql and PHP,I have a simple table in my DB, looks something like this:
id jim bob bill
1 20 30 15
2 20 35 15
etc, what i need to do is get the sums for each column (I can do that), then output each field_name with the corresponding sum sorted desc like this
bob 65
jim 40
bill 30
here's the query to get the sums:
<?php
$query = 'SELECT SUM(jim) AS jim, SUM(bob) AS bob, SUM(bill) AS bill FROM week';
$result = mysql_query($query) or die(mysql_error());
and I use this to get the field names:
$values['jim']['name'] = mysql_field_name($result, 0);
$values['bob']['name'] = mysql_field_name($result, 1);
$values['bill']['name'] = mysql_field_name($result, 2);
?>
I can get the sums, I can get the field names, but I can't get them into separate rows and sort them by the sums. I've tried a number of things but I'm stumped!!! Any ideas, hints?
Thanks, Tom
6 2570
Hi,
I'm using Mysql and PHP,I have a simple table in my DB, looks something like this:
id jim bob bill
1 20 30 15
2 20 35 15
etc, what i need to do is get the sums for each column (I can do that), then output each field_name with the corresponding sum sorted desc like this
bob 65
jim 40
bill 30
here's the query to get the sums:
<?php
$query = 'SELECT SUM(jim) AS jim, SUM(bob) AS bob, SUM(bill) AS bill FROM week';
$result = mysql_query($query) or die(mysql_error());
and I use this to get the field names:
$values['jim']['name'] = mysql_field_name($result, 0);
$values['bob']['name'] = mysql_field_name($result, 1);
$values['bill']['name'] = mysql_field_name($result, 2);
?>
I can get the sums, I can get the field names, but I can't get them into separate rows and sort them by the sums. I've tried a number of things but I'm stumped!!! Any ideas, hints?
Thanks, Tom
Hi,
Im not sure how much experence you have with data bases but i think your problem may come from how you have used your table. Every time you add a new person you'll need to add a new field which is not normally how things are done (btw, i'm no database expert)
However if you set up your table like this: -
ID Name Number
-
1 bob 20
-
2 jim 30
-
3 bill 15
-
4 bob 20
-
5 jim 35
-
6 bill 15
-
then you could do a query something like this: -
SELECT Name, Sum(Number) AS NumberTotal
-
FROM week
-
GROUP BY Name
-
ORDER BY Name DESC;
-
you'll get exactly what ya want
Do ya follow? tell us how ya go :)
Tweek
Sorry i made a small mistake... we should order by number like: -
SELECT Name, Sum(Number) AS NumberTotal
-
FROM week
-
GROUP BY Name
-
ORDER BY Sum(Number) DESC;
-
Just a hint, if you have MS Access installed you can setup a test database that looks the same as your MySQL one and try out some queries in there.
or you can get/buy frontends that work like Access for your MySQL databases which can be useful tools for developing and testing
Tweek
Hi!! Well, once I get the database set up I wouldn't be adding anyone new to it. That's why the field names are people's names. I'd just be adding new numbers to each name, then wanting to output the sums with each field name. I've been thinking I could get each sum and corresponding field name into an array and sort it by sum but have had no luck!! But I might be able to make it work your way, I'll give it a try. Thanks for taking the time!!!
Tom
Sorry i made a small mistake... we should order by number like: -
SELECT Name, Sum(Number) AS NumberTotal
-
FROM week
-
GROUP BY Name
-
ORDER BY Sum(Number) DESC;
-
Just a hint, if you have MS Access installed you can setup a test database that looks the same as your MySQL one and try out some queries in there.
or you can get/buy frontends that work like Access for your MySQL databases which can be useful tools for developing and testing
Tweek
Well, I changed my database and tried your query, looks like it's going to work just fine for me!!! Thanks so much for you help!!!
Tom
Well, I changed my database and tried your query, looks like it's going to work just fine for me!!! Thanks so much for you help!!!
Tom
If I could ask one more question, I've changed the database as you suggested and the above problem seems to be solved, but now I want to be able to get only the last entry for each name. I've tried all sorts of group by, order by, max(id) etc but with no effect, any ideas?
Well, once I get the database set up I wouldn't be adding anyone new to it. That's why the field names are people's names.
like I said, I'm no database expert and there's probably a better way of doing it. In fact, in a big database, you'd probably have a entirly separate table for storing people
...I want to be able to get only the last entry for each name. I've tried all sorts of group by, order by, max(id) etc but with no effect, any ideas?
Well I can think of a few ways to do it, like you could get the last id of each person and store those values in an array and then look those up in another query.
However, I'd be temped to do something really simple (and I'm assuming this table stores number of hours worked per week or something like that) and add a field like "WeekNumber" to the table like (where WeekNumber is the week of the year): -
ID Name Number WeekNumber
-
1 bob 20 1
-
2 jim 30 1
-
3 bill 15 1
-
4 bob 20 2
-
5 jim 35 2
-
6 bill 15 2
-
Then i could lookup any week when ever I wanted like: - SELECT * FROM week WHERE WeekNumber = <Some Number>;
A query like: -
SELECT * FROM week
-
WHERE WeekNumber = (SELECT Max(WeekNumber) FROM week);
-
would give the latest week for each person: -
4 bob 20 2
-
5 jim 35 2
-
6 bill 15 2
-
Im at work at the moment (so i can't really test PHP code here) but perhaps someone else could think of another way, I can't think of a way to do it entirely in a query with your current table
Tweek
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Karin Jensen |
last post by:
Hi
I am running a PHP program that connects to an Access 2000 database
via ODBC:
$results = odbc_exec($connection_id, $sql_select);
Is it possible to sort the contents of $results? I wish to...
|
by: suzy |
last post by:
hello.
how can i sort data in a dataset? all the examples i have seen on msdn, etc
are sorting a dataview. this works fine, but i want to return the results
in xml and the dataview doesn't...
|
by: Mike MacSween |
last post by:
tblProductions one to many to tblEvents
tblEvents contains StartDate
I want a report where the data are grouped by tblProductions.ProdID, and
sorted by the earliest date in each Production.
...
|
by: Mike MacSween |
last post by:
tblCourses one to many to tblEvents.
A course may have an intro workshop (a type of event), a mid course
workshop, a final exam. Or any combination. Or something different in the
future.
At...
|
by: luca varani |
last post by:
I would like to sort the results of a crosstab query by the aggregate
function it automatically generates (total of the values in each column of
the crosstab).
If I simply put "ascending" in the...
|
by: Neil Hindry |
last post by:
I wonder if you can help me.
I have setup an address-book database in Access XP. I have the first name &
surname as separate fields. As I wanted to sort my database by surname and
then by first...
|
by: Brett Romero |
last post by:
I have a dataset with one table, which has four columns. All are of
type INT. I need to convert this dataset into a dataview so I can sort
on the last three columns. I may sort one of the three...
|
by: Dylan Parry |
last post by:
Hi folks,
I have a database that contains records with IDs like "H1, H2, H3, ...,
Hn" and these refer to local government policy numbers. For example, H1
might be "Housing Policy 1" and so on....
|
by: Tim Olson |
last post by:
I'm using a form to pass parameters to a query, with results displayed
in a report. Currently, I'm using parameters as filter criteria in
the query, and everything is running smoothly (or so it...
|
by: russot00 |
last post by:
I have 3 drop down menus that are used in a search to locate restaurants in a db. All of the drop down menus function, a search can be submitted with any combination of drop downs and the results are...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |