How to get SUM of f3 but Grouped by f2
I want to get all comments count grouped by category. eg
f1 f2 f3
php hello 34
php hello 34
php hell开发者_如何转开发o 34
php world 23
php world 23
php world 23
asp world1 43
asp world1 43
asp world1 43
How to get sum of f3 but grouped by f2?
So not 34 + 34 + 34 +23 ... but 34 + 23 + 43
EDIT: f2 is unique
SELECT SUM(f3) FROM `tablename` GROUP BY f2, f3
Okay I thought it all over.. and basically if i do understand correct you want the sum for each distinct set.
So not 34 + 34 + 34 + 23 + 23 + 23 + 43 + 43 + 43 ... BUT 34 + 23 + 43
In order to that you need to trick MySQL with the following query :
SELECT
SUM(tot.invalid_votes) AS total
FROM (SELECT DISTINCT QV, invalid_votes FROM `results`) tot
This will return the SUM of the Distinct values based on QV field.
Table :
QV invalid_votes
0544 5
0544 5
0544 5
0545 6
0545 6
based on the database information you provided i get the following result 11
The only way I can think of to get the sum of the three different values is
Select sum (distinct f3) from yourtable
I hope it makes sense to you, because I'm still trying to figure out what you want.
Based on the pastebin you posted,
this should work
SELECT QV, SUM(invalid_votes) FROM `results` GROUP BY QV
I get the following result
QV SUM(invalid_votes)
0544 15
0545 12
精彩评论