Home ›
Calculate the sum of an entire result set of values with a single SQL queryCalculate the sum of an entire result set of values with a single SQL query
Submitted by Benjamin Melançon on October 10, 2008 - 3:49am
As tested in phpMyAdmin:
SELECT SUM( `value` )
FROM `dru_ec_product_zing_auction` AS za
LEFT JOIN `dru_node` AS n ON za.nid = n.nid
WHERE STATUS > 0;
Drupalized:
SELECT SUM(value) FROM {ec_product_zing_auction} AS za LEFT JOIN {node} AS n ON za.nid = n.nid WHERE status > 0
As actually used in a Drupal module, zing_auction:
<?php
$query = "SELECT SUM(value) FROM {ec_product_zing_auction} AS za LEFT JOIN {node} AS n ON za.nid = n.nid WHERE status > 0";
$totestvalall = db_result(db_query($query));
?>
Resolution
Searched words:
MySQL SUM
Comments
Post new comment