方法如下:
如数据:
id name
1a
1b
2c
2d
3e
3f
3g
3h
select count(t.counts) from
(select id,count(*) counts from 表名 group by id) t
这样得到的结果就是3。
您好,单个select语句实现MySQL查询统计次数的方法用处在哪里呢?用处太多了,比如一个成绩单,你要查询及格得人数与不及格的人数,怎么一次查询出来?MySQL查询统计次数简单的语句肯定是这样了:
select a.name,count_neg,count_plus from
(select count(id) as count_plus,name from score2 where score >=60 group by name) a,
(select count(id) as count_neg,name from score2 where score <=60 group by name) b
where a.name=b.name
即必须至少用2个语句。
今天刚好碰到发现mysql支持if,那就创造性的用if来实现吧:
select name, sum(if(score>=60,1,0)),sum(if(score<60,1,0)) from score2 group by name
单个select语句实现MySQL查询统计次数的方法简单吧。
原理就是大于60,就赋值为1,那么sum就是计数了。
国产数据库产品稳定性应该说已经接近oracle了,目前在政务、金融领域已经大规模开始应用和替换,去IOE,比如南大通用Gbase,互联网行业的阿里腾讯也都有自己的数据库,国产数据库还是不错的欢迎分享,转载请注明来源:内存溢出
评论列表(0条)