您的位置:首页技术文章
文章详情页

mysql - thinkphp一个查询语句的实现

【字号: 日期:2022-06-19 13:09:03浏览:46作者:猪猪

问题描述

mysql - thinkphp一个查询语句的实现

获取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();

相关文章: