简体   繁体   中英

calculating and adding column values in mysql php

I have the following table

AffID    l      Commision
-------------------------
MW001              5

MW004              10

MW001              25

MW001              5

MW004              5

I want to submit a query that counts the amount in a column. So for example the output i am looking for is :

MW001 - 35

The query i am using now does not working correctly, it counts the actual amount of records.

$sql = "SELECT COUNT(*) as c FROM toutcome WHERE affID = '" . $_SESSION['affID'] . "'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo  $row['c'] ;

Try this query

select sum(Commission) from toutcome where affID = 'MW001'

So your query should be

$sql = "SELECT SUM(Commission) as c FROM toutcome WHERE affID = '" . $_SESSION['affID'] . "'";

to see all of the different commissions you can sum and group by

SELECT SUM(commission) FROM toutcome GROUP BY affID

so see a specific one just do this

SELECT SUM(commission) FROM toutcome WHERE affID = whicheveroneyouwant

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM