SUM/Total of values from a DB Query output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cptuser
    New Member
    • Mar 2007
    • 30

    SUM/Total of values from a DB Query output

    Hi,
    I have a database query that outputs values, such as the cost of cars.

    car_id car_value

    1 20000
    2 5000
    3 1000
    4 40000
    and so on....

    How do I then write some PHP code, to add all these "car_values " from the result of the query. So if usingt he exampel above, I want to sum up, find the total, 20000+5000+1000 +40000=66000

    Hope someone can help. My knowledge of PHP is very little.
  • hoopy
    New Member
    • Feb 2009
    • 88

    #2
    Hi,

    Try the SUM syntax in SQL such as:

    Code:
    SELECT SUM(car_value) AS total FROM tblcars;
    So in PHP:

    Code:
    $qry = mysql_query(" SELECT SUM(car_value) AS total FROM tblcars ");
    $row = mysql_fetch_assoc($qry);
    echo $row['total'];
    Cheers.

    Comment

    • cptuser
      New Member
      • Mar 2007
      • 30

      #3
      Thanks for the reply. Unforutantely, I want the SUM of the MAXimum values, so essential something like this
      SELECT SUM (MAX(car_value) )... which obviously wouldn't work.
      Hence the reason why I thought first to do the MAX query using MySQL and then use a SUM function of some sort in PHP.

      Can someone assist?

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by cptuser
        Thanks for the reply. Unforutantely, I want the SUM of the MAXimum values, so essential something like this
        SELECT SUM (MAX(car_value) )... which obviously wouldn't work.
        Hence the reason why I thought first to do the MAX query using MySQL and then use a SUM function of some sort in PHP.

        Can someone assist?
        What do you mean the MAXimum values? I don't understand.

        SUM( ) will return the total of the given column, which is what I thought you wanted?

        Comment

        • cptuser
          New Member
          • Mar 2007
          • 30

          #5
          Just as per my mySQL query, which obviously does not work, I'm aware of the SUM() function in MySQL, however, I don't want just the SUM() of all rows, I need to first get the maximum values from the table and then add them up. I know this would be hard to do in MySQL and would need a double query or something.

          It's best to ignore MySQL for now, what I'm after rather is doing SUM using PHP.
          So, like my orginal post, sum up, (add up the totals) from a recordset results list. I need to do this in PHP and not MYSQL, as it won't work in MySQL for various reason (especially because I'm need to first retreive the Maximum values because the DB table has foreign key in their linking to another table)

          Hope you can help, with how to does this in PHP, adding up the values from a recordset.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by cptuser
            Just as per my mySQL query, which obviously does not work, I'm aware of the SUM() function in MySQL, however, I don't want just the SUM() of all rows, I need to first get the maximum values from the table and then add them up. I know this would be hard to do in MySQL and would need a double query or something.

            It's best to ignore MySQL for now, what I'm after rather is doing SUM using PHP.
            So, like my orginal post, sum up, (add up the totals) from a recordset results list. I need to do this in PHP and not MYSQL, as it won't work in MySQL for various reason (especially because I'm need to first retreive the Maximum values because the DB table has foreign key in their linking to another table)

            Hope you can help, with how to does this in PHP, adding up the values from a recordset.
            Well, I assume you're looping through the results returned through MySQL? Why not just have a variable (set to 0) and then with each loop add to the variable the current rows price.

            Code:
            $total_price = 0;
            while ( $row = mysql_fetch_array( $resource ) )
            {
                $total_price += $row['price'];
            }

            Comment

            • cptuser
              New Member
              • Mar 2007
              • 30

              #7
              This is what I have and it's not working:

              Code:
              	    <?php do {           $totalBids = 0; ?>
              	      <?php echo $row_totalBids['highestBids']; ?>
              	      <?php } while ($row_totalBids = mysql_fetch_assoc($totalBids))
              		      $totalBids += $row_totalBids['highestBids']?>

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by cptuser
                This is what I have and it's not working:

                Code:
                	    <?php do {           $totalBids = 0; ?>
                	      <?php echo $row_totalBids['highestBids']; ?>
                	      <?php } while ($row_totalBids = mysql_fetch_assoc($totalBids))
                		      $totalBids += $row_totalBids['highestBids']?>
                Why a do while() loop?

                mysql_* functions expect a valid result resource - a value returned via mysql_query(). $totalBids is not one.

                Check out a PHP MySQL tutorial.

                Comment

                Working...