2

I have multiple columns in a mySQL table. Three of the columns are named i100s, i60s and i25s and what I want to do is get the sum of all three entries . currently I have this code

   '$query= "SELECT SUM(i100s),SUM(i60s),SUM(i25s) AS tkit FROM event WHERE acc='100' " ; 
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_assoc($result) ;
    $total =  $row['tkit'];' 

But it is not returning the correct result.

3 Answers 3

6

Combined sum of all three columns?

If so, simply add them up:

'$query= "SELECT SUM(i100s) + SUM(i60s) + SUM(i25s) AS tkit FROM event WHERE acc='100' " ;
Sign up to request clarification or add additional context in comments.

Comments

0
SELECT (SUM(i100s) + SUM(i60s) + SUM(i25s)) AS tkit ...

This would return the sum of the sum of the columns.

Comments

0

There will be different methods.... and the payments are payed on method_1, method_2, method_3. So I want to calculate the sum of one particular method say 1000.. for all method types.

Table: payment_test

id method_1 payment_1 method_2 payment_2 method_3 payment_3
1  1000     100       1001     200       1001     100
2  1011     100       1000     100       1000     100
3  1010     200       1001     100       1010     200
4  1000     400       1010     500       1001     100


SELECT SUM(
    CASE WHEN method_1 ='1000' THEN payment_1 ELSE 0 END
    + CASE WHEN method_2 ='1000' THEN payment_2 ELSE 0 END
    + CASE WHEN method_3 ='1000' THEN payment_3 ELSE 0 END) as payment
FROM payment_test

Output:

payment
700

Comments

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.