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

得到分組后的總條數。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM