将表和他自己连接~
select *
from tb_comment a left join tb_comment b
on b.c_fatherid=a.cid
得到右侧有空的结果,空的地方count(*)会被统计,用count(column)不会被统计~
1)count(*)所有行进行统计,包含值为null的行。
2)count(column)会对指定列具有的行数进行统计,除去值为NULL的行。
3)count(1)与count(*) 的效果是一样的。
select a.cid, count(b.cid)
from tb_comment a left join tb_comment b
on b.c_fatherid=a.cid
group by a.cid;
结果如下: