sql查询计算agroup计数的总和

时间:2014-10-11 08:03:00

标签: php mysql

字段 id,party,votes

我的查询得到像这样的o / p

 partyname    bjp      aap         congress   

  votes        1        2              2    

但我也需要总票数。以下是我的预期产量。

  BJP         aap           congress       total

   1             2            2             5

SELECT RES.PARTYNAME, COUNT( RES.CONSTITUENCY )
        AS VOTESCOUNT FROM voter_count RES JOIN (SELECT CONSTITUENCY, MAX( VOTES )
        AS VOTES FROM voter_count GROUP BY CONSTITUENCY)MAXS USING ( VOTES, CONSTITUENCY )
        GROUP BY PARTYNAME LIMIT 0 , 1000";

2 个答案:

答案 0 :(得分:1)

看看这个

SELECT SUM(something)AS something,SUM(else)AS else FROM stuff

只是想知道如何获得SUM

答案 1 :(得分:0)

根据你的表格,

SELECT SUM(partyname) AS total_parties, SUM(votes) AS total_votes FROM table_name;

这是你想要的吗?你的问题不明确&查询与描述无关......