使用Thinkphp解决group和count一起使用的问题

使用tp,group和count无法得到想要的sql语句。

M('Report')->group('begin')->where($term)->count();

SELECT COUNT(*) AS tp_count FROM `qdb_report` WHERE ( `type` = 1 ) AND ( `branch` = 59 ) GROUP BY begin LIMIT 1

 

改成子查询:

$subQuery = M('Report')->group('begin')->where($term)->select(false);
$data['rows'] = M('Report')->table($subQuery . ' a')->count();

SELECT COUNT(*) AS tp_count FROM ( SELECT * FROM `qdb_report` WHERE ( `type` = 1 ) AND ( `branch` = 59 ) GROUP BY begin  ) a LIMIT 1  

得到分组后的总条数。

posted @ 2020-08-17 09:58  1O(∩_∩)O1  阅读(1849)  评论(0编辑  收藏  举报