php中如何用SQL查询实现分组后按某字段统计总数
2014-04-01 21:06:30   来源:   评论:0 点击:

SQl子查询简单应用如下:php中如何用SQL查询实现分组后按某字段统计总数$sql = "select C *,P * from `ctable` as C left join `p

SQl子查询简单应用如下:php中如何用SQL查询实现分组后按某字段统计总数

$sql = "select C.*,P.* from `ctable` as C left join `ptable` as P on P.`pid` = C.`pid` group by C.`pid` order by `cid` desc";

详解:联表查询,以ctable的pid字段进行分组查询.

实现上面sql查询结果的记录总数 

$sql = "select count(C.`cid`) as `c` from (select * from `ctable` group by pid) as C left join `ptable` as P on P.`pid` = C.`pid`";

详解,此语句用到了sql子查询,先使用子查询对ctable进行分组查询,然后对分组后的结果集进行统计.

SQl子查询了解:http://www.php114.net/2011/1115/122.html

相关热词搜索:

上一篇:SQL语句大全三(sql提升篇)
下一篇:Mysql全文索引作用与开启方法