MySQL query, SUM of multiple columns
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'];'
Bu开发者_运维百科t it is not returning the correct result.
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' " ;
SELECT (SUM(i100s) + SUM(i60s) + SUM(i25s)) AS tkit ...
This would return the sum of the sum of the columns.
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
精彩评论