繁体   English   中英

PHP代码选择最大值并在表中显示最大值的计数

[英]php code to select max value and display count of max value in table

表格名称投票者字段名称-派对名称,州,方向,候选人名称,地区,表格中的投票日期是明智的

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   

我正在从表中选择州,方向和地区明智条件的最高选票。 并在下方显示投票。

现在,我想问您如何获得上述查询结果,并在每个方向上显示我的表格中有多少最大值。 请建议我……我只需要显示最大方向数即可。

对于前方向投票方,东向5000 bjp东向8000 aap,那么东向只有1个最大值,然后显示计数1。.plz告诉我....

<table class="bordered">
    <tr> 
    <?php           
        $sqlst = "SELECT DISTINCT state FROM 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 FROM voter_count where 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) AS Vote,direction FROM voter_count where state = '$state' AND district = '$district' AND 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>

不能100%确定最终表的外观,但这只是一个开始(您不需要第一个查询,可以通过一个查询来完成所需的操作):

  SELECT  MAX(votes) AS Vote,direction, state, district FROM voter_count
  group by direction, state, district

作为提及的评论之一,请参考以下有关如何使用分组依据的链接: http : //www.w3schools.com/sql/sql_groupby.asp

如果您使用单个表进行存储,我不清楚您要做什么。

我建议你

制作两个表,分别用于id,state,direction,district

另一个是id,fkid(区的id),partyname,candidatename,votes

这使您的任务更加轻松

然后

 $con  =    db_connect();
 $str   =   "select a.state,a.direction,a.district
             from tabel1 as a 
             inner join tabel2 as b
             on b.fkid=a.id ";

    $result =   mysql_query($str,$con);

    while($arr=mysql_fetch_array($result))
    {
        echo "<pre>";print_r($arr); //prints all values as a  formatted array Use these values in your tabel.
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM