lunarbytes Posted April 15, 2012 Share Posted April 15, 2012 I am trying to sum a column from a qurey using the top values only. Using the following line I get the result ( 7 top distance answer) for that column. $distance = mysql_query("SELECT distance FROM competition WHERE JPersonID=$row[iD] ORDER BY distance DESC LIMIT 7"); $row[iD] is from an earlier query so that only values related to the above query is called. As can be seen I order the result from highest to lowest limiting the result to 7 records. Now I need to add these results, to get a total distance. Using the 'SUM(distance) As xxx' does not give me the right result in the above query. (It counts all the distances relating to $row[iD]. I only require the top 7 results. Please can someone help? The result displayed should be as follows: (The code for this is not required. I just need to know how to sum the 7 jumps using the query) Total Distance Jump 1 Jump 2 Jump3 Jump4 Jump5 Jump6 Jump7 32.4 5.4 5.3 5.1 4.8 4.5 3.9 3.4 Link to comment Share on other sites More sharing options...
justsomeguy Posted April 16, 2012 Share Posted April 16, 2012 I guess the obvious answer would be to get the result in whatever language you're working with (PHP, ASP, etc), and loop through the result set to add the values up. Link to comment Share on other sites More sharing options...
lunarbytes Posted April 17, 2012 Author Share Posted April 17, 2012 I guess the obvious answer would be to get the result in whatever language you're working with (PHP, ASP, etc), and loop through the result set to add the values up. Thanks. I found the answer using a different query and it now does what it supposed to do. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.