文章詳情頁
mysql - thinkphp一個查詢語句的實現
瀏覽:101日期:2022-06-19 13:09:03
問題描述
獲取catid的個數,獲取當state等于2時state的個數,并且還要按照pid分組。
請問一個sql語句怎么實現上面的要求。
問題解答
回答1:m('test')->alias(’a’)->field('count(catid),(select count(1) from test where state=2 and pid=a.pid)')->group('pid')->select();回答2:
sql語句:
SELECT COUNT(’catid’) WHERE state=’2’ GROUP BY ’pid’;
tp代碼:
$model->count(’catid’)->where(’state=2’)->group(’pid’)->select();
相關文章:
排行榜
