otixz Posted April 27, 2008 Share Posted April 27, 2008 Hi, I need a msyql query that will calculate for the total of a given condition.... select sum(select amount from tblinquiries where inq_date = '22/04/2008') from tblinquiries what I want to do is to get the TOTAL of all the amount of 22/4/2008 data from my tblinquiries table. please help...what query can I use? Link to comment https://forums.phpfreaks.com/topic/103126-get-total-in-mysql-query/ Share on other sites More sharing options...
paul2463 Posted April 27, 2008 Share Posted April 27, 2008 you could do it in php $total = 0; $query = "SELECT amount FROM tblinquiries WHERE inq_date = '22/04/2008'"; $result = mysql_query($query) or die ("Error in query " . mysql_error()); while ($row = mysql_fetch_assoc($result) { $total = $total + $row['amount']; } echo $total; Link to comment https://forums.phpfreaks.com/topic/103126-get-total-in-mysql-query/#findComment-528221 Share on other sites More sharing options...
Lumio Posted April 27, 2008 Share Posted April 27, 2008 you could do it in php $total = 0; $query = "SELECT amount FROM tblinquiries WHERE inq_date = '22/04/2008'"; $result = mysql_query($query) or die ("Error in query " . mysql_error()); while ($row = mysql_fetch_assoc($result) { $total = $total + $row['amount']; } echo $total; Why don't you show him mysql_num_rows? The easiest way with MySQL is SELECT COUNT(amount) as totalCount FROM tblinquiries WHERE inq_date = '22/04/2008'; Link to comment https://forums.phpfreaks.com/topic/103126-get-total-in-mysql-query/#findComment-528224 Share on other sites More sharing options...
paul2463 Posted April 27, 2008 Share Posted April 27, 2008 i always thought that sum() had to be used with a groupby command as well I now know different Link to comment https://forums.phpfreaks.com/topic/103126-get-total-in-mysql-query/#findComment-528228 Share on other sites More sharing options...
Lumio Posted April 27, 2008 Share Posted April 27, 2008 You are right it was COUNT instead of SUM. Link to comment https://forums.phpfreaks.com/topic/103126-get-total-in-mysql-query/#findComment-528230 Share on other sites More sharing options...
otixz Posted April 27, 2008 Author Share Posted April 27, 2008 Hi, The COUNT was just able to get the number of how many data are in the database. It did not calculate the TOTAL SUM of the amount in the database. Link to comment https://forums.phpfreaks.com/topic/103126-get-total-in-mysql-query/#findComment-528233 Share on other sites More sharing options...
AndyB Posted April 27, 2008 Share Posted April 27, 2008 http://www.tizag.com/mysqlTutorial/mysqlsum.php Link to comment https://forums.phpfreaks.com/topic/103126-get-total-in-mysql-query/#findComment-528238 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.