• <legend id='AGdLL'><style id='AGdLL'><dir id='AGdLL'><q id='AGdLL'></q></dir></style></legend>

    <small id='AGdLL'></small><noframes id='AGdLL'>

    1. <tfoot id='AGdLL'></tfoot>

        <i id='AGdLL'><tr id='AGdLL'><dt id='AGdLL'><q id='AGdLL'><span id='AGdLL'><b id='AGdLL'><form id='AGdLL'><ins id='AGdLL'></ins><ul id='AGdLL'></ul><sub id='AGdLL'></sub></form><legend id='AGdLL'></legend><bdo id='AGdLL'><pre id='AGdLL'><center id='AGdLL'></center></pre></bdo></b><th id='AGdLL'></th></span></q></dt></tr></i><div id='AGdLL'><tfoot id='AGdLL'></tfoot><dl id='AGdLL'><fieldset id='AGdLL'></fieldset></dl></div>

          <bdo id='AGdLL'></bdo><ul id='AGdLL'></ul>

      1. Yii:获取每月热门用户和年末热门用户的SQL查询

        时间:2023-06-06
          <legend id='SL8fg'><style id='SL8fg'><dir id='SL8fg'><q id='SL8fg'></q></dir></style></legend>
          <i id='SL8fg'><tr id='SL8fg'><dt id='SL8fg'><q id='SL8fg'><span id='SL8fg'><b id='SL8fg'><form id='SL8fg'><ins id='SL8fg'></ins><ul id='SL8fg'></ul><sub id='SL8fg'></sub></form><legend id='SL8fg'></legend><bdo id='SL8fg'><pre id='SL8fg'><center id='SL8fg'></center></pre></bdo></b><th id='SL8fg'></th></span></q></dt></tr></i><div id='SL8fg'><tfoot id='SL8fg'></tfoot><dl id='SL8fg'><fieldset id='SL8fg'></fieldset></dl></div>

          <small id='SL8fg'></small><noframes id='SL8fg'>

            <tbody id='SL8fg'></tbody>

          <tfoot id='SL8fg'></tfoot>
            <bdo id='SL8fg'></bdo><ul id='SL8fg'></ul>

                  本文介绍了Yii:获取每月热门用户和年末热门用户的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我是一个 yiibie,我有一个问题,我有一个名为 user_join_event 的表,它有 id(PK) ngo_id(FK) event_id(FK) date_created date_modified 作为对象,我想获得每个月的 5 个用户 ID(在此表中重复次数最多).像1月前5名用户,2月前5名用户等等,然后是这张表中年底前5名的用户,就像一年结束后,它应该再次给出整个月的前5名用户.请帮我解决这个问题,如何为此编写SQL查询,谢谢.

                   公共函数 actionAllstats(){$this->layout='main';$user=UserJoinEvent::model()->findAll(array('条件' =>'YEAR(date_created)=:year 和 MONTH(date_created)=:month','参数' =>数组(':year'=>2015, ':month'=>$yourMonth),'选择'='=>'user_id','组'=>'user_id','订单'=>'COUNT(*) DESC','限制' =>5));$this->render('allstats', array("user"=>$user));}

                  这是我的视图文件

                  <?phpforeach($user 作为 $show){for($i = 1 ; $i <=12 ; $i++){if($yourMonth == $i){$model = User::model()->findByAttributes(array('id'=>$show->user_id,));如果(isset($模型)){回声'<h3>'.$show->user_id .' - ' .$model->用户名.'</h3>';}别的 {回声'<h3>'.$show->user_id .' - 未找到用户名</h3>';}}}}?>

                  解决方案

                  你可以起诉这个查询获得用户的年度

                   $user=UserJoinEvent::model()->findAll(array('条件' =>'YEAR(date_create)=:year','参数' =>数组(':year'=>2015)'选择'='=>'user_id','组'=>'user_id','订单'=>'COUNT(*) DESC','限制' =>5));

                  并且对于这个月,您可以使用此查询从 1 到 12 进行循环,并使用循环中的值设置 $yourMonth

                   $user=UserJoinEvent::model()->findAll(array('条件' =>'YEAR(date_create)=:year 和 MONTH(date_create)=:month','参数' =>数组(':year'=>2015, ':month'=>$yourMonth)'选择'='=>'user_id','组'=>'user_id','订单'=>'COUNT(*) DESC','限制' =>5));

                  控制器动作

                  公共函数 actionAllstats(){$this->layout='main';$myCurrYear = 日期(Y")$userYear=UserJoinEvent::model()->findAll(array('条件' =>'YEAR(date_create)=:year','参数' =>数组(':year'=>$myCurrYear)'选择'='=>'user_id','组'=>'user_id','订单'=>'COUNT(*) DESC','限制' =>5));$this->render('allstats', array("userYear"=>$userYear));}

                  查看

                  这个视图不是基于良好的实践,因为在视图中使用了对模型的访问.是为了引导您了解与问题相关的一些问题的尝试.

                  <?php//这十二个月for($month = 1 ; $month <=12 ; $month++) {<div>$user=UserJoinEvent::model()->findAll(array('条件' =>'YEAR(date_created)=:year 和 MONTH(date_created)=:month','参数' =>数组(':year'=>2015, ':month'=>$month),'选择'='=>'user_id','组'=>'user_id','订单'=>'COUNT(*) DESC','限制' =>5));foreach($user as $show) {$model = User::model()->findByAttributes(array('id'=>$show->user_id,));如果(isset($模型)){回声'<h3>'.$show->user_id .' - ' .$model->用户名.'</h3>';}别的 {回声'<h3>'.$show->user_id .' - 未找到用户名</h3>';}}

                  }?><div><?php//年份foreach($userYear as $show) {$model = User::model()->findByAttributes(array('id'=>$show->user_id,));如果(isset($模型)){回声'<h3>'.$show->user_id .' - ' .$model->用户名.'</h3>';}别的 {回声'<h3>'.$show->user_id .' - 未找到用户名</h3>';}}?>

                  I am a yiibie, I have a question that I have a table named user_join_event which has id(PK) ngo_id(FK) event_id(FK) date_created date_modified as objects, and I want to get the 5 users Id's(which are repeated the most in this table) for every month. Like January top 5 users, Feb top 5 users and so on and then the top 5 users at the end of the year from this table, like once the year is completed it should give again the top 5 users from all over the month. Please help me with this, how to write an SQL query for this, thank you.

                   public function actionAllstats()
                              {
                  
                          $this->layout='main';
                          $user=UserJoinEvent::model()->findAll(array(
                              'condition' => 'YEAR(date_created)=:year and MONTH(date_created)=:month',
                              'param' => array(':year'=>2015, ':month'=>$yourMonth),
                              'select'=>'user_id',
                              'group'=>'user_id',
                              'order'=>'COUNT(*) DESC',
                              'limit' =>  5
                           ));  
                          $this->render('allstats', array("user"=>$user));
                              }
                  

                  and this is my view file

                  <div>
                      <?php
                  
                          foreach($user as $show)
                          {
                              for($i = 1 ; $i <=12 ; $i++)
                              {
                                  if($yourMonth == $i)
                                  {
                                      $model = User::model()->findByAttributes(array('id'=>$show->user_id,));
                             if (isset($model)) 
                                 {
                                 echo '<h3>' . $show->user_id .  ' - ' . $model->username . '</h3>';
                             } 
                             else {
                                 echo '<h3>' . $show->user_id .  ' - Username not found</h3>';
                             } 
                                  }
                              }
                  
                          }
                  
                          ?>
                      </div>
                  

                  解决方案

                  For the Year you can sue this query for getting the users

                         $user=UserJoinEvent::model()->findAll(array(
                              'condition' => 'YEAR(date_create)=:year',
                              'params' => array(':year'=>2015)
                              'select'=>'user_id',
                              'group'=>'user_id',
                              'order'=>'COUNT(*) DESC',
                              'limit' =>  5
                           ));  
                  

                  and for the month you can do a loop from 1 to 12 using this query and set $yourMonth withe the value in loop

                         $user=UserJoinEvent::model()->findAll(array(
                              'condition' => 'YEAR(date_create)=:year and MONTH(date_create)=:month',
                              'params' => array(':year'=>2015, ':month'=>$yourMonth )
                              'select'=>'user_id',
                              'group'=>'user_id',
                              'order'=>'COUNT(*) DESC',
                              'limit' =>  5
                           ));  
                  

                  Controller action

                  public function actionAllstats()
                  {
                  
                      $this->layout='main';
                      $myCurrYear = date("Y")
                      $userYear=UserJoinEvent::model()->findAll(array(
                          'condition' => 'YEAR(date_create)=:year',
                          'params' => array(':year'=>$myCurrYear)
                          'select'=>'user_id',
                          'group'=>'user_id',
                          'order'=>'COUNT(*) DESC',
                          'limit' =>  5
                       ));  
                      $this->render('allstats', array("userYear"=>$userYear));
                  }
                  

                  View

                  This view in not based on good pratice, because use access to model inside a view. is a trial for lead you to understand some of the problem related with question.

                  <?php    // this for the twelve month
                  
                      for($month = 1 ; $month <=12 ; $month++) {
                      <div>
                          $user=UserJoinEvent::model()->findAll(array(
                              'condition' => 'YEAR(date_created)=:year and MONTH(date_created)=:month',
                              'params' => array(':year'=>2015, ':month'=>$month),
                              'select'=>'user_id',
                              'group'=>'user_id',
                              'order'=>'COUNT(*) DESC',
                              'limit' =>  5
                           ));    
                          foreach($user as $show) {
                              $model = User::model()->findByAttributes(array('id'=>$show->user_id,));
                              if (isset($model)) {
                                  echo '<h3>' . $show->user_id .  ' - ' . $model->username . '</h3>';
                              } 
                               else {
                                  echo '<h3>' . $show->user_id .  ' - Username not found</h3>';
                              } 
                          }
                      </div>            
                      }
                  ?>
                  
                  
                  <div>   
                      <?php // the for the year 
                          foreach($userYear as $show) {
                              $model = User::model()->findByAttributes(array('id'=>$show->user_id,));
                              if (isset($model)) {
                                  echo '<h3>' . $show->user_id .  ' - ' . $model->username . '</h3>';
                              } 
                               else {
                                  echo '<h3>' . $show->user_id .  ' - Username not found</h3>';
                              } 
                          }
                      ?>
                  </div>
                  

                  这篇关于Yii:获取每月热门用户和年末热门用户的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:#1005 - 无法创建表“table_name"(错误号:150) 下一篇:创建“数据库监视器"时无法打开 lib '/us

                  相关文章

                  最新文章

                  <legend id='qtAex'><style id='qtAex'><dir id='qtAex'><q id='qtAex'></q></dir></style></legend>
                    <bdo id='qtAex'></bdo><ul id='qtAex'></ul>
                • <i id='qtAex'><tr id='qtAex'><dt id='qtAex'><q id='qtAex'><span id='qtAex'><b id='qtAex'><form id='qtAex'><ins id='qtAex'></ins><ul id='qtAex'></ul><sub id='qtAex'></sub></form><legend id='qtAex'></legend><bdo id='qtAex'><pre id='qtAex'><center id='qtAex'></center></pre></bdo></b><th id='qtAex'></th></span></q></dt></tr></i><div id='qtAex'><tfoot id='qtAex'></tfoot><dl id='qtAex'><fieldset id='qtAex'></fieldset></dl></div>

                  <tfoot id='qtAex'></tfoot>

                    1. <small id='qtAex'></small><noframes id='qtAex'>