使用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  

得到分组后的总条数。

原文地址:https://www.cnblogs.com/laijinquan/p/13515986.html