欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mysql中Count函数的正确使用

程序员文章站 2022-04-13 15:41:36
备注: 直接使用Count(*)或Count(1)这些大家基本都会,主要是Count函数还可以加满足表达式的统计:express 关于Count函数表达式的用法,目前个人只知道2种: a:使用:Count(表达式 Or null) b:使用:Count(Case when 表达式 then 1 E... ......

备注: 直接使用count(*)或count(1)这些大家基本都会,主要是count函数还可以加满足表达式的统计:express


关于count函数表达式的用法,目前个人只知道2种:

a:使用:count(表达式 or null)

b:使用:count(case when 表达式 then 1 end) 或者 count(case when 表达式 then 1 else null end)

如:

select userid,
count(parentid=0 or null),
count(case when parentid=0 then 1 end),
count(case when parentid=0 then 1 else null end)
from usermessage
where userid in (511946,656015,1541157,3465768,5049530,5112483,5210074,5372797) group by userid