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
Searched words:
MySQL SUM
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
More like this
- Looking at SQL to fix broken queries in a Drupal environment
- Drupal won't set values to NULL with db_query %d system
- Agaric Utility function: set default values in an array
- Imagefield images disappears entirely when set to use Lightbox2 in CCK field display settings
- Fastest way to get just one result from a database table in Drupal


Comments
Post new comment