0

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'] ;
1
  • 1
    perhaps the name of the function should clue you in: COUNT() is doing exactly what you're getting. Perhaps you should use SUM() instead. Commented Aug 15, 2014 at 13:53

2 Answers 2

1

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'] . "'";
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

$sql = "SELECT SUM(AffCommission) FROM toutcome WHERE affID = '" . $_SESSION['affID'] . "' and DATE_SUB(NOW(), INTERVAL 1 WEEK) "; $result = mysql_query($sql); $row = mysql_fetch_array($result); echo $row['AffCommission'] ;
@user3485335 do you have a date created in your table?
@user3485335 sure thing! don't forget to mark an answer as accepted to keep the community as is :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.