jkkenzie Posted May 10, 2008 Share Posted May 10, 2008 I have a database with 2 fields (Country and Value). i cant figure out how to sum values for particular country and put them in variables; like COuntry1- sum=45 country2- sum=50 this is my code so far: //Get the country with the highest rank value $getresults4 = mysql_query("SELECT DISTINCT strCountry FROM tblcountries WHERE Par_Id='$parid' AND projectid='$proid' ORDER BY strCountry"); $count4=0; while($rows4 = mysql_fetch_array($getresults4)) { $countryname4[]=$rows4['strCountry']; $count4++; } $count41=0; while($count41<=$count4) { $getresults5 = mysql_query("SELECT Par_Value FROM tblcountries WHERE Par_Id='$parid' AND projectid='$proid' AND strCountry='$countryname[$count41]' ORDER BY strCountry"); while($rows5 = mysql_fetch_array($getresults5)) { $sum=$sum+$rows5['Par_Value']; } $count41++; } Quote Link to comment https://forums.phpfreaks.com/topic/105002-sum-database-values-at-once/ Share on other sites More sharing options...
Zane Posted May 10, 2008 Share Posted May 10, 2008 to get your sum for par_value $getresults5 = mysql_query("SELECT Par_Value, SUM(Par_Value) as ParSum FROM tblcountries WHERE then you don't need $sum=$sum+$row etc etc you'll have $row['ParSum'] Quote Link to comment https://forums.phpfreaks.com/topic/105002-sum-database-values-at-once/#findComment-537470 Share on other sites More sharing options...
jkkenzie Posted May 10, 2008 Author Share Posted May 10, 2008 Thanks now i have the sum per country and i can display, BUT i need to get the name of the country with the highest SUM???? How? Thanks again. Regards, Joseph Quote Link to comment https://forums.phpfreaks.com/topic/105002-sum-database-values-at-once/#findComment-537474 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.