将所有值一起添加到SQL SELECT中

将所有值一起添加到SQL SELECT中

问题描述:

How do I add all the amounts together so that instead of this select script returning all the individual withdrawal amounts return one number of them all added together?

<?php
$end_date = strtotime($nextdate);
$start_date = strtotime($statement_date);
$username = $usr['username'];
$result = mysql_query("SELECT * FROM `usr_withdraw` WHERE `timestamp` > '$start_date' AND `timestamp` < '$end_date' AND `username` = '$username'")or die(mysql_error());

while($balance = mysql_fetch_array( $result )) { 
    echo $balance['amount'];
}
?>

如何将所有 amount code>添加到一起,以便不再使用此select脚本返回所有 个人提款金额是否归还了其中一个数字? p>

 &lt;?php 
 $ end_date = strtotime($ nextdate); 
 $ start_date = strtotime($ statement_date); 
 $ username = $ usr ['username  ']; 
 $ result = mysql_query(“SELECT * FROM`usr_withdraw` WHERE`timestamp`&gt;'$ start_date'AND`timestamp`&lt;'$ end_date'AND`username` ='$ username'”)或死 (mysql_error()); 
 
而($ balance = mysql_fetch_array($ result)){
 echo $ balance ['amount']; 
} 
?&gt; 
  code>  pre>  
  div>

To return only 1 row with the aggregate sum of all amounts use the sql sum() function.

SELECT sum(amount) FROM `usr_withdraw` WHERE ...

Do

$result = mysql_query("SELECT SUM(amount) FROM `usr_withdraw` WHERE `timestamp` > '$start_date' AND `timestamp` < '$end_date' AND `username` = '$username'")or die(mysql_error());

But also keep in mind that these mysql_* functions are out of PHP from version 7 on, then your code will be soon out of date. The ideal is using mysqli_* functions or PDO.