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;
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.
SELECT SUM(money) AS total FROM table WHERE name='$name'