mysql count if 一起用

当你瘦下来之后,你会发现原来你的丑,跟减肥并没有关系。

 

mysql  里面的count  是我们比较常用的函数

select count(*) from table_1;

select count(id) from table_2;

 

但是有些时候,需要配合 if  进行统计查询

select count(if(age<18), true, null) as young_count, count(if(age>=18, true, null)) as adult_count    from table_3;

 

在mysql 里面  if  使用方式表达

if( exp, true, null)   最后一个值是null ,不是 false

 

稍微注意一下就ok,相信你已经记住了。

posted @ 2021-08-26 08:56  方达达  阅读(774)  评论(0编辑  收藏  举报