0
点赞
收藏
分享

微信扫一扫

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

飞鸟不急 2022-06-13 阅读 63

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

得到分组后的总条数。


举报

相关推荐

0 条评论