警予:在SQL查询数据的重复(Yii: Repetition of data in sql quer

2019-11-04 03:31发布

我是一个Yiibie,我有,我有一个查询这使我每月的基础上,非政府组织的等级前5名非政府组织的问题。 但问题是,非政府组织的名称是前5名重复,我想非政府组织的的名字不应该在排名前5的非政府组织的名单重复。 下面是这个视图文件。

              <div class="main">
    <div class="banner">
<img src="<?php echo Yii::app()->request->baseurl;?>/img/stat1.jpg" style="width: 1170px">
</div><!--banner ending here--><br>
    <div class="container" style="background-color:#7AC5CD">
        <div class="row">
            <div class="col-md-6">
        <h3 style='text-align:center; text-decoration: underline;font-family: sans-serif; color: black'>Top 5 Ngo's month wise</h3>
        <br>
    <?php
    for($month = 1 ; $month <=12 ; $month++) 
    {
        '<br>' ;
        $dateObj   = DateTime::createFromFormat('!m', $month);
        $monthName = $dateObj->format('F'); 
        echo "<h3 style='text-align:center;color:black;'> " . $monthName . "</h3>"; 

        $user=UserRateReviewNgo::model()->findAll(array(
            'condition' => 'YEAR(date_created)=:year and MONTH(date_created)=:month',
            'params' => array(':year'=>2016, ':month'=>$month),
            'select'=>'DISTINCT rate,ngo_id',
        'order'=>'rate DESC',
        'limit' =>  5
         )); 
        $val = 100;
        foreach($user as $show) {

            $model = Ngo::model()->findByAttributes(array('id'=>$show->ngo_id,));
            if (isset($model)) {
                echo "<div><h4><a href='".Yii::app()->request->baseUrl."/ngo/ngopage?id=$model->id'><p style='color:black;font-family:Lucida Sans Unicode;'>" . $model->ngo_name ."</p></a></h4></div>


                    <div class='progress'>
                    <div class='progress-bar  progress-bar-danger progress-bar-striped active' role='progressbar'
                    aria-valuenow='" . $val ."' aria-valuemin='0' aria-valuemax='100' style='width: ". $val ."%;'>" .  $val .
                    "</div>
                    </div>";
                    $val = $val -21.23;


    }  }}

    ?>
        </div>

             <div class="col-md-5 col-md-offset-1">   
                 <h3 style='text-align:center; text-decoration: underline;font-family: sans-serif; color: black'>Top 5 Ngo's of the year</h3>
<br>   
 <?php // the for the year 
    $val = 100;
        foreach($userYear as $show)
            {
            $model = Ngo::model()->findByAttributes(array('id'=>$show->ngo_id,));
            if (isset($model)) 
                {
               echo "<div><h4><a href='".Yii::app()->request->baseUrl."/ngo/ngopage?id=$model->id'><p style='color:black; font-family:Lucida Sans Unicode;'>" . $model->ngo_name ."</p></a></h4></div>


                    <div class='progress'>
                    <div class='progress-bar progress-bar-success progress-bar-striped active' role='progressbar'
                    aria-valuenow='" . $val ."' aria-valuemin='0' aria-valuemax='100' style='width: ". $val ."%;'>" .  $val .
                    "</div>
                    </div>";
                    $val = $val -17.96;

            } 

        }
    ?>
</div>
            </div>
        </div><!--main ending here-->

</div><!--Container ending here-->

请帮我看看这个,谢谢。

Answer 1:

如果你想最多的前5级速率的非政府组织,你应该使用此查询

$user=UserRateReviewNgo::model()->findAll(array(
        'condition' => 'YEAR(date_created)=:year and MONTH(date_created)=:month',
        'params' => array(':year'=>2016, ':month'=>$month),
        'select'=>'max(rate),ngo_id',
        'group' => 'ngo_id',
    'order'=>'rate DESC',
    'limit' =>  5
     )); 


文章来源: Yii: Repetition of data in sql query
标签: php yii