亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MySql統(tǒng)計函數(shù)COUNT的具體使用詳解

 更新時間:2022年08月14日 10:56:05   作者:靖節(jié)先生  
本文主要介紹了MySql統(tǒng)計函數(shù)COUNT的具體使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1. COUNT()函數(shù)概述

COUNT() 是一個聚合函數(shù),返回指定匹配條件的行數(shù)。開發(fā)中常用來統(tǒng)計表中數(shù)據(jù),全部數(shù)據(jù),不為NULL數(shù)據(jù),或者去重數(shù)據(jù)。

2. COUNT()參數(shù)說明

COUNT(1):統(tǒng)計不為NULL 的記錄。
COUNT(*):統(tǒng)計所有的記錄(包括NULL)。

COUNT(字段):統(tǒng)計該"字段"不為NULL 的記錄。
1.如果這個字段是定義為not null的話,一行行地從記錄里面讀出這個字段,判斷不能為null,按行累加。
2.如果這個字段定義允許為null的話,判斷到有可能是null,還要把值取出來在判斷一下,不是null才累加。

COUNT(DISTINCT 字段):統(tǒng)計該"字段"去重且不為NULL 的記錄。

-- MySql統(tǒng)計函數(shù)count測試
-- 創(chuàng)建用戶表,新增測試數(shù)據(jù)
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;

-- 統(tǒng)計數(shù)據(jù):7條數(shù)據(jù),統(tǒng)計所有的記錄(包括NULL)。
select count(*) from user;

-- 統(tǒng)計數(shù)據(jù):7條數(shù)據(jù),統(tǒng)計不為NULL 的記錄。
select count(1) from user;

-- 統(tǒng)計數(shù)據(jù):5條數(shù)據(jù),COUNT(字段):統(tǒng)計該"字段"不為NULL 的記錄,注意是null不是空''字符串
select count(name) from user;

-- 統(tǒng)計數(shù)據(jù):5條數(shù)據(jù),COUNT(DISTINCT 字段):統(tǒng)計該"字段"去重且不為NULL 的記錄。
select count(distinct name) from user;

3. COUNT()判斷存在

SQL不再使用count,而是改用LIMIT 1,讓數(shù)據(jù)庫查詢時遇到一條就返回,不要再繼續(xù)查找還有多少條了,業(yè)務(wù)代碼中直接判斷是否非空即可。
select 1 from emp LIMIT 1;效率是最高的,尤其是需要limit限制行數(shù),很容易忽略。

-- SQL查找是否"存在"
-- 員工表,存在則進(jìn)行刪除
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數(shù)據(jù)
測試數(shù)據(jù):https://blog.csdn.net/m0_37583655/article/details/124385347

-- cahxun 
select * from emp ;

-- 時間:1.082s,數(shù)據(jù):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,數(shù)據(jù):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,數(shù)據(jù):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,讓數(shù)據(jù)庫查詢時遇到一條就返回,不要再繼續(xù)查找還有多少條了,業(yè)務(wù)代碼中直接判斷是否非空即可
-- 時間:0.001s,數(shù)據(jù):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()阿里開發(fā)規(guī)范

1.【強(qiáng)制】不要使用 count(列名)或 count(常量)來替代 count(),count()是 SQL92 定義的標(biāo) 準(zhǔn)統(tǒng)計行數(shù)的語法,跟數(shù)據(jù)庫無關(guān),跟 NULL 和非 NULL 無關(guān). 說明:count(*)會統(tǒng)計值為 NULL 的行,而 count(列名)不會統(tǒng)計此列為 NULL 值的行.

2.【強(qiáng)制】count(distinct col) 計算該列除 NULL 之外的不重復(fù)行數(shù),注意 count(distinct col1, col2) 如果其中一列全為 NULL,那么即使另一列有不同的值,也返回為 0.

在這里插入圖片描述

 到此這篇關(guān)于MySql統(tǒng)計函數(shù)COUNT的具體使用詳解的文章就介紹到這了,更多相關(guān)MySql統(tǒng)計函數(shù)COUNT內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論