Jump to content

mysql sort problem


tom_b

Recommended Posts

I hope I can explain this O.K.  I have a database with 3 columns.  I can input numbers into it, run a query and output the last entry in each column, then run a second query and output the total of each column so the output looks like this:

 

a  10    50

b  15    60

c  12    55

 

it works fine but I'm stumped as how to sort the output by totals.  Any ideas?

 

Thanks, Tom

Link to comment
https://forums.phpfreaks.com/topic/38399-mysql-sort-problem/
Share on other sites

These queries select the last entry from column a, then the total of that column.  I have similar queries for columns b and c.  Everything works fine, I just can't sort the output based on the separate queries that total columns a, b and c.

 

$query = "select a from week where id=(select max(id)from week)";   

$result=mysql_query($query);

$num=mysql_numrows($result);

 

    while ($row = mysql_fetch_array($result)) {

    ?><tr><td> <?php

    $a= $row['a'];

    echo "<font color ='blue' font size = '3'>$a<br>";

}

$result = mysql_query ("select sum(a) from week");   

 

    $sum = mysql_fetch_row($result);

    $sum = $sum[0]; 

    echo "<font color ='blue' font size = '3'>$sum<br>";

 

Thanks, I do appreciate your time!!  Tom

 

Link to comment
https://forums.phpfreaks.com/topic/38399-mysql-sort-problem/#findComment-184191
Share on other sites

Well, you can at least reduce it to two queries...

 

$values = array();

$query = 'SELECT a, b, c FROM week ORDER BY id DESC LIMIT 1';
$result = mysql_query($query) or die(mysql_error());

$values['a']['current'] = mysql_result($result, 0, "a");
$values['b']['current'] = mysql_result($result, 0, "b");
$values['c']['current'] = mysql_result($result, 0, "c");

$query = 'SELECT SUM(a) AS a, SUM(b) AS b, SUM(c) AS c FROM week';
$result = mysql_query($query) or die(mysql_error());

$values['a']['sum'] = mysql_result($result, 0, "a");
$values['b']['sum'] = mysql_result($result, 0, "b");
$values['c']['sum'] = mysql_result($result, 0, "c");

 

You should then be able to use the example here:

 

http://us2.php.net/array_multisort#id2766574

 

to sort your results.

Link to comment
https://forums.phpfreaks.com/topic/38399-mysql-sort-problem/#findComment-184212
Share on other sites

O.K., the queries you wrote make sense (except I'm not sure what 'current' means!!).  I'll have to check out that link more closely tomorrow.  Would I then put my data into an array to sort it?  Haven't done anything like that before, but it does make sense.  Thanks again for you help!!

 

Tom

Link to comment
https://forums.phpfreaks.com/topic/38399-mysql-sort-problem/#findComment-184253
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.