ThinkPHP统计查询

统计查询
在应用中我们经常会用到一些统计数据,例如当前所有(或者满足某些条件)的用户数、所有用户的最大
积分、用户的平均成绩等等,ThinkPHP为这些统计操作提供了一系列的内置方法,包括:

    //http://localhost/thinkphp323/index.php/home/User/max_score    
    public function max_score(){
        $User = M("User"); // 实例化User对象
        // 获取用户数:
        $userCount = $User->count();
        echo "userCount : $userCount <br>";
        // 或者根据字段统计:
        $userCount = $User->count("id");
        echo "userCount id : $userCount <br>";
        // 获取用户的最大积分:
        $maxScore = $User->max('score');
        echo "maxScore : $maxScore <br>";
        // 获取积分大于0的用户的最小积分:
        $minScore = $User->where('score>0')->min('score');
        echo "minScore : $minScore <br>";
        // 获取用户的平均积分:
        $avgScore = $User->avg('score');
        echo "avgScore : $avgScore <br>";
        // 统计用户的总成绩:
        $sumScore = $User->sum('score');
        echo "sumScore : $sumScore <br>";
    }
userCount : 3 
userCount id : 3 
maxScore : 100 
minScore : 23 
avgScore : 74.3333 
sumScore : 223 

 

posted @ 2017-11-27 19:35  sky20080101  阅读(85)  评论(0)    收藏  举报