COUNT(expr)
官方文档
Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement. The result is a BIGINT value.
返回的是expr中非NULL的行数
对于含有NULL值得列慎用
举例
表结构及数据
DROP TABLE IF EXISTS `tb` ;
CREATE TABLE `tb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` varchar(20) DEFAULT NULL,
`c2` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into tb(c1) values ('str_1'),(NULL),('str_1'),('str_1');
count(*):表中所有行。共4行
count(c1):c1列不为NULL的行数。共3行
count(c2)
:c2列不为NULL的行数。共0行
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26250550/viewspace-1104564/
,如需转载,请注明出处,否则将追究法律责任。