473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

sorting query results

18
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
Feb 27 '07 #1
6 2570
Tweek
17
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:

Expand|Select|Wrap|Line Numbers
  1. ID    Name   Number
  2. 1     bob       20
  3. 2     jim       30
  4. 3     bill      15
  5. 4     bob       20
  6. 5     jim       35
  7. 6     bill      15
  8.  
then you could do a query something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT Name, Sum(Number) AS NumberTotal
  2. FROM week
  3. GROUP BY Name
  4. ORDER BY Name DESC;
  5.  

you'll get exactly what ya want


Do ya follow? tell us how ya go :)

Tweek
Feb 27 '07 #2
Tweek
17
Sorry i made a small mistake... we should order by number like:

Expand|Select|Wrap|Line Numbers
  1. SELECT Name, Sum(Number) AS NumberTotal
  2. FROM week
  3. GROUP BY Name
  4. ORDER BY Sum(Number) DESC;
  5.  
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
Feb 27 '07 #3
tom_b
18
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
Feb 27 '07 #4
tom_b
18
Sorry i made a small mistake... we should order by number like:

Expand|Select|Wrap|Line Numbers
  1. SELECT Name, Sum(Number) AS NumberTotal
  2. FROM week
  3. GROUP BY Name
  4. ORDER BY Sum(Number) DESC;
  5.  
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
Feb 27 '07 #5
tom_b
18
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?
Feb 28 '07 #6
Tweek
17
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):

Expand|Select|Wrap|Line Numbers
  1. ID    Name   Number   WeekNumber
  2. 1     bob       20      1
  3. 2     jim       30      1
  4. 3     bill      15      1
  5. 4     bob       20      2
  6. 5     jim       35      2
  7. 6     bill      15      2
  8.  
Then i could lookup any week when ever I wanted like:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM week WHERE WeekNumber = <Some Number>;

A query like:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM week
  2. WHERE WeekNumber = (SELECT Max(WeekNumber) FROM week);
  3.  
would give the latest week for each person:

Expand|Select|Wrap|Line Numbers
  1. 4     bob       20      2
  2. 5     jim       35      2
  3. 6     bill      15      2
  4.  
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
Feb 28 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
4
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...
1
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. ...
8
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...
2
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...
3
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...
7
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...
9
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....
3
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.