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

2023-04-04数据库问题
4

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

问题描述

我是一个 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查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

SQL 子句“GROUP BY 1"是什么意思?意思是?

SQL 子句“GROUP BY 1"是什么意思?意思是?

What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果

MySQL groupwise MAX() 返回意外结果

MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁

MySQL SELECT 按组最频繁

MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同

Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)...
2024-04-16 数据库问题
13

MySQL GROUP BY DateTime +/- 3 秒

MySQL GROUP BY DateTime +/- 3 秒

MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)...
2024-04-16 数据库问题
14

热门文章

1ORA-01747: 无效的 user.table.column、table.column 或列规范 2ORA-01461: 只能为插入到 LONG 列而绑定 LONG 值-查询时发生 3INSERT 语句与 FOREIGN KEY 约束冲突 4MySql 错误:无法更新存储函数/触发器中的表,因为它已被调用此存储函数/触发器的语句使用 5MySQL:将逗号分隔的列表拆分为多行 6sqlite3 在数据库中插入和读取 BLOB 数据 7ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 8MySQL 错误:UPDATE 和 LIMIT 的错误使用

热门精品源码

最新VIP资源

1多功能实用站长工具箱html功能模板 2多风格简历在线生成程序网页模板 3论文相似度查询系统源码 4响应式旅游景点宣传推广页面模板 5在线起名宣传推广网站源码 6酷黑微信小程序网站开发宣传页模板 7房产销售交易中介网站模板 8小学作业自动生成程序