![]() |
SQL Statement help - Sum - Resource ID #81
I'm coding a page in a new hack I'm building and am trying to sum a column using a where condition. Here's the code I'm using:
Code:
$pledgeamount=$vbulletin->db->query("SELECT SUM(donation_amount) from " . TABLE_PREFIX . "donations where donation_sport='$football'"); I've done hacks before for Vbulletin, but never had to use a Sum statement. Is there something special I need to do? |
Instead of using the query() function, use query_first() which will also fetch the results as an array.
|
Thanks for the reply. I was able to find a work-around (make it work) by looking at the code for vbBookie...i figured they were adding something in there. With that said, here's code that works (maybe not efficient, but a starting point for somebody in the same position I was).
Code:
$pledgetotals=$db->query_read("SELECT SUM(donation_amount) pledges FROM " . TABLE_PREFIX . "donations where donation_sport='$football'"); |
query_first() basically calls query_read() and fetch_array() to save time. :)
|
And calls a free_result as well. :)
|
The reason the first query does not work is because the result field name ends up something like SUM(donation_amount), not donation_amount as you expected. With SUM you generally use AS.
SELECT SUM(my_field) AS SomeName FROM X GROUP BY Y; |
All times are GMT. The time now is 08:16 AM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|