本文介绍了Yii:sql 查询中的数据重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Yiibie,我有一个问题,我有一个查询,根据非政府组织的评级,它为我提供了每个月排名前 5 的非政府组织.但问题是 Ngo 的名字在前 5 名中重复,我希望 ngo 的名字不应该在前 5 名非政府组织的列表中重复.这是视图文件.

 

<div class="banner"><img src="<?php echo Yii::app()->request->baseurl;?>/img/stat1.jpg" style="width: 1170px"></div><!--横幅在此结束--><br><div class="container" style="background-color:#7AC5CD"><div class="row"><div class="col-md-6"><h3 style='text-align:center;文字装饰:下划线;字体系列:无衬线;颜色:黑色'>Ngo 的月明智前 5 名</h3><br><?phpfor($month = 1 ; $month ';$dateObj = DateTime::createFromFormat('!m', $month);$monthName = $dateObj->format('F');echo "<h3 style='text-align:center;color:black;'> " .$monthName ."</h3>";$user=UserRateReviewNgo::model()->findAll(array('条件' =>'YEAR(date_created)=:year 和 MONTH(date_created)=:month','参数' =>数组(':year'=>2016, ':month'=>$month),'select'='DISTINCT rate,ngo_id','order'='rate DESC','限制' =>5));$val = 100;foreach($user as $show) {$model = Ngo::model()->findByAttributes(array('id'=>$show->ngo_id,));如果(isset($模型)){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>

";$val = $val -21.23;} }}?>

<div class="col-md-5 col-md-offset-1"><h3 style='text-align:center;文字装饰:下划线;字体系列:无衬线;颜色:黑色'>年度最佳 Ngo 5 强</h3><br><?php//年份$val = 100;foreach($userYear as $show){$model = Ngo::model()->findByAttributes(array('id'=>$show->ngo_id,));如果(isset($模型)){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>

";$val = $val -17.96;}}?>

</div><!--这里主要结束--></div><!--容器在此结束-->

请帮我解决这个问题,谢谢.

解决方案

如果你想要 5 个最高评价的非政府组织,你应该使用这个查询

$user=UserRateReviewNgo::model()->findAll(array('条件' =>'YEAR(date_created)=:year 和 MONTH(date_created)=:month','参数' =>数组(':year'=>2016, ':month'=>$month),'选择'='=>'max(rate),ngo_id','组' =>'ngo_id','order'='rate DESC','限制' =>5));

I am a Yiibie and I have a question that I have a query which gives me the top 5 Ngo's of every month based on the rating of the ngo. But the problem is that Ngo's name are repeated in the top 5 and I want that ngo's name should not be repeated in the list of top 5 ngo's. Here is the view file.

              <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-->

Please help me with this, thank you.

解决方案

If you want the 5 most rate ngo you should use this query

$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:sql 查询中的数据重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 16:37