PHP Sum of Mysql行值

PHP Sum of Mysql行值

问题描述:

figure 1. sum all value of money   
    id  date           name     money //sample items only
    1   March 5 2014   matt      50 //records to add
    2   March 5 2014   john      10 //records to add
    3   March 2 2014   matt      50 //records to add

figure 2. sum all value of money when search matt
    id  date           name     money //sample items only
    1   March 5 2014   matt      50 //records to add
    2   March 5 2014   john      10
    3   March 2 2014   matt      50 //records to add

Hi guys can you help me i dont know the code how to Add the rows value in my sql database.(see sample above)

i have this code which will count how many records i have

<input type="text" name="namesearch" size="5"  tabindex="1" />

<input type="submit" name='click' value='Search' />


if(isset($_POST['click'])){


    $name= $_POST['namesearch'];
    $query = "SELECT COUNT(*) AS total FROM table WHERE name='$name'"; 
    $result = mysql_query($query); 
    $values = mysql_fetch_assoc($result); 
    $num_item= $values['total']; 
    echo "Number of Records Found # ".$num_item;
 图1.汇总所有货币金额
 id日期名称货币//仅限样本项目
  1月5日2014年亚马50 //记录添加
 2 2014年3月5日约翰10 //记录添加
 3月2日2014年亚马50 //记录添加
 
figure 2.在搜索时汇总所有货币价值 matt 
 id date name money //仅限样品
 1月5日2014年mat 50 //要添加的记录
 2 2014年3月5日约翰10 
 3月2日2014年mat 50 //记录添加
  代码>  pre> 
 
 

大家好,你们可以帮助我吗?我不知道如何在我的sql数据库中添加行值的代码。(参见上面的示例) p>

我有这个代码,它将计算我有多少记录 p>

 &lt; input type =“text”name =“namesearch”size =“5”tabindex =“1”  /&gt; 
 
&lt; input type =“submit”name ='click'value ='Search'/&gt; 
 
 
if(isset($ _ POST ['click'])){
 
  
 $ name = $ _POST ['namesearch']; 
 $ quer  y =“SELECT COUNT(*)AS total FROM table WHERE name ='$ name'”;  
 $ result = mysql_query($ query);  
 $ values = mysql_fetch_assoc($ result);  
 $ num_item = $ values ['total'];  
 echo“找到的记录数#”。$ num_item; 
  code>  pre> 
  div>

use this as query

$query = "SELECT COUNT(*) AS total, SUM(money) AS total_money FROM table WHERE name='$name'"; 

use this to view the result

$num_item = $values['total']; 
$total_value = $values['total_money'];
echo "Number of Records Found # ".$num_item;
echo "Total Money ".$total_value

Since my MySQL server is missing after an OS upgrade, don't trust the syntax (lol). Try this:

SELECT COUNT(*), SUM(`money`) AS `total_money` FROM `my_table` GROUP BY `name`;

This should(?) give you the count of rows for each name and it's sum of total money.

Use MySQL SUM Function

SELECT SUM(money) AS total FROM table WHERE name='$name'