mysql count的作用是什么

38次阅读
没有评论

共计 3573 个字符,预计需要花费 9 分钟才能阅读完成。

本篇内容主要讲解“mysql count 的作用是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“mysql count 的作用是什么”吧!

mysql count 是一个聚合函数,用于返回指定匹配条件的行数;count 函数的使用语法如“select count(*) from user;”,表示统计所有的记录,包括 NULL。

1. COUNT()函数概述

COUNT() 是一个聚合函数,返回指定匹配条件的行数。开发中常用来统计表中数据,全部数据,不为 NULL 数据,或者去重数据。

2. COUNT()参数说明

COUNT(1):统计不为 NULL 的记录。
COUNT(*):统计所有的记录(包括 NULL)。

COUNT(字段):统计该 字段 不为 NULL 的记录。

如果这个字段是定义为 not null 的话,一行行地从记录里面读出这个字段,判断不能为 null,按行累加。

如果这个字段定义允许为 null 的话,判断到有可能是 null,还要把值取出来在判断一下,不是 null 才累加。

COUNT(DISTINCT 字段):统计该 字段 去重且不为 NULL 的记录。

-- MySql 统计函数 count 测试
--  创建用户表,新增测试数据
CREATE TABLE `user` (
 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT  ID 主键 ,
 `name` varchar(64) DEFAULT NULL COMMENT  姓名 ,
 `sex` varchar(8) DEFAULT NULL COMMENT  性别 ,
 `age` int(4) DEFAULT NULL COMMENT  年龄 ,
 `born` date DEFAULT NULL COMMENT  出生日期 ,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT= 用户表

INSERT INTO `category`.`user`(`id`, `name`, `sex`, `age`, `born`) VALUES (1,  % 张三 % ,  男 , 22,  2022-04-22
INSERT INTO `category`.`user`(`id`, `name`, `sex`, `age`, `born`) VALUES (2,  李四 ,  女 , 12,  2022-04-01
INSERT INTO `category`.`user`(`id`, `name`, `sex`, `age`, `born`) VALUES (3,  王小二 ,  女 , 12,  2022-04-28
INSERT INTO `category`.`user`(`id`, `name`, `sex`, `age`, `born`) VALUES (4,  赵四 ,  男 , 23,  2022-04-28
INSERT INTO `category`.`user`(`id`, `name`, `sex`, `age`, `born`) VALUES (5,  ,  女 , 23,  2022-04-28
INSERT INTO `category`.`user`(`id`, `name`, `sex`, `age`, `born`) VALUES (6, NULL,  女 , 60,  2022-04-28
INSERT INTO `category`.`user`(`id`, `name`, `sex`, `age`, `born`) VALUES (7, NULL,  女 , 61,  2022-04-28

select * from user;

--  统计数据:7 条数据,统计所有的记录(包括 NULL)。
select count(*) from user;

--  统计数据:7 条数据,统计不为 NULL  的记录。
select count(1) from user;

--  统计数据:5 条数据,COUNT(字段):统计该 字段 不为 NULL  的记录,注意是 null 不是空 字符串
select count(name) from user;

--  统计数据:5 条数据,COUNT(DISTINCT  字段):统计该 字段 去重且不为 NULL  的记录。
select count(distinct name) from user;

3. COUNT()判断存在

SQL 不再使用 count,而是改用 LIMIT 1,让数据库查询时遇到一条就返回,不要再继续查找还有多少条了, 业务代码中直接判断是否非空即可。
select 1 from emp LIMIT 1; 效率是最高的,尤其是需要 limit 限制行数,很容易忽略。

-- SQL 查找是否 存在 
--  员工表, 存在则进行删除
drop table if EXISTS emp;
create table emp(
 id int unsigned primary key auto_increment,
 empno mediumint unsigned not null default 0,
 empname varchar(20) not null default  ,
 job varchar(9) not null default  ,
 mgr mediumint unsigned not null default 0,
 hiredate datetime not null,
 sal decimal(7,2) not null,
 comn decimal(7,2) not null,
 depno mediumint unsigned not null default 0
);

--  新增 cehsi 数据
测试数据:https://blog.csdn.net/m0_37583655/article/details/124385347

-- cahxun 
select * from emp ;

--  时间:1.082s,数据:5000000
explain select count(*) from emp;

id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1  SIMPLE Select tables optimized away

--  时间:1.129s,数据:5000000
explain select count(1) from emp;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1  SIMPLE Select tables optimized away

--  时间:1.695s,数据:5000000
explain select 1 from emp;
id select_type table partitions type possible_keys key  key_len ref rows  filtered Extra
1  SIMPLE emp idx_emp_depno 3 4981060 100.00 Using index

-- SQL 不再使用 count,而是改用 LIMIT 1,让数据库查询时遇到一条就返回,不要再继续查找还有多少条了, 业务代码中直接判断是否非空即可
--  时间:0.001s,数据:5000000
explain select 1 from emp LIMIT 1;
id select_type table partitions type possible_keys key  key_len ref rows  filtered Extra
1  SIMPLE emp idx_emp_depno 3 4981060 100.00 Using index

4. COUNT()阿里开发规范

1.【强制】不要使用 count(列名)或 count(常量)来替代 count(),count()是 SQL92 定义的标 准统计行数的语法, 跟数据库无关, 跟 NULL 和非 NULL 无关. 说明:count(*)会统计值为 NULL 的行, 而 count(列名)不会统计此列为 NULL 值的行.

2.【强制】count(distinct col) 计算该列除 NULL 之外的不重复行数, 注意 count(distinct col1, col2) 如果其中一列全为 NULL, 那么即使另一列有不同的值, 也返回为 0.

到此,相信大家对“mysql count 的作用是什么”有了更深的了解,不妨来实际操作一番吧!这里是丸趣 TV 网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-13发表,共计3573字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)