Monday 15 September 2014

mysql - php code to select max value and display count of max value in table -



mysql - php code to select max value and display count of max value in table -

table name voter fields name - partyname,state,direction,candidatename,district,votes date in table wise

bjp maharashtra nagpur-east nitin gadkari nagpur 3000 congress maharashtra nagpur-east lucky nagpur 2500 bjp maharashtra nagpur-south rakesh nagpur 12000 congress maharashtra nagpur-south shyam nagpur 15000

i selecting max votes table on status of state,direction , district wise. , display vote below.

now want inquire how above query result , display count of how much max value in table on each direction. plz suggest me...... need display count of max value direcion wise.

for ex--- direction votes party east 5000 bjp east 8000 aap there 1 max value in east direction display count 1.. plz tell me....

<table class="bordered"> <tr> <?php $sqlst = "select distinct state voter_count"; $resultst = mysql_query($sqlst); while($rowst = mysql_fetch_array($resultst,mysql_assoc)) { ?> <td> <table class="bordered"> <thead> <tr> <th colspan="3"><?php echo $rowst['state']; ?></th> </tr> <?php $state = $rowst["state"]; $sqlco = "select distinct district,direction voter_count state = '$state'"; $resultco = mysql_query($sqlco); while($rowco = mysql_fetch_array($resultco,mysql_assoc)) { ?> <tr> <?php $district = $rowco['district']; $direction = $rowco['direction']; $sqlvotes = "select max(votes) vote,direction voter_count state = '$state' , district = '$district' , direction = '$constituency'"; $resultvotes = mysql_query($sqlvotes); while($rowvotes = mysql_fetch_array($resultvotes,mysql_assoc)) { ?> <th><?php echo $rowvotes['vote']; ?></th> <th><?php echo $rowvotes['direction']; ?></th> </tr> <?php } } ?> </thead> </table> </td> <?php } ?> </tr> </table>

not 100% sure final table needs like, start (you don't need first query, can want 1 query):

select max(votes) vote,direction, state, district voter_count grouping direction, state, district

as 1 of comments mentioned refer link how utilize grouping by: http://www.w3schools.com/sql/sql_groupby.asp

php mysql

No comments:

Post a Comment