TP5按月份分组查询当年内的数据字段总和

// 获取当前年份
$year = date('Y');
// 查询本年数据, 数据库内字段类型为时间戳
$start_time = strtotime($year . '-01-01 00:00:00');
$end_time = strtotime($year . '-12-31 23:59:59');

// 分组查询每个月的浏览量和收藏量的月度总值
$list = Db::name('test')
    ->whereTime('createtime', 'between', [$start_time, $end_time])
    ->field("SUM(`page_view`) as page_view,SUM(`collection`) as collection,FROM_UNIXTIME(createtime,'%Y-%m') as month")
    ->group('month')
    ->order('month asc')
    ->select();

var_dump($list);

记录备忘

风影OvO

风影OvO, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA 4.0协议进行授权 | 转载请注明原文链接

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐