0

my query getting o/p like this

partyname    bjp      aap         congress   

votes        1        2              2          

but i also need sum of row. below is my expected output.

all the partyname and votes comes from database and it is dynamic.

for ex

 BJP         aap           congress       total

  1             2            2             5


<?php                
$sqltoatl = "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";

$resulttoatl = mysql_query($sqltoatl);

while ($rowtoatl = mysql_fetch_array($resulttoatl,MYSQL_ASSOC)) {
?>  

<th><?php echo $rowtoatl['PARTYNAME']; ?></th>      
<td><?php echo $rowtoatl['VOTESCOUNT']; ?></td> 
<?php } ?>  

2 Answers 2

1

Not knowing your database schema I don't quite understand the reason for the complicated vote calculation you're doing.

Maybe I'm missing something, but if what you want is a number of votes per party and a total then maybe something like this is what you want?

SELECT PARTYNAME, SUM(votes) AS VOTES FROM voter_count GROUP BY Partyname
UNION ALL
SELECT 'Total', SUM(VOTES) AS VOTES FROM voter_count;

It could look like this SQL Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

Something like this should work for you:

SELECT p.partyId, p.partyName, COUNT(IFNULL(v.bjp,0)) bjpTotal, COUNT(IFNULL(v.aap,0)) aapTotal, COUNT(IFNULL(v.congress,0)) congressTotal, 
      (COUNT(IFNULL(v.bjp,0)) + COUNT(IFNULL(v.aap,0)) + COUNT(IFNULL(v.congress,0))) partyTotal
FROM parties p
LEFT JOIN votes v
    ON p.partyId = v.partyId
GROUP BY p.partyId, p.partyName

2 Comments

sir but my partyname is not fix it comes different from database and also votes comes from database
Namrehs you need to explain things a little bit, don't just throw out code

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.