mysqlcount的作用是什么

本篇內(nèi)容主要講解“MySQL count的作用是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“mysql count的作用是什么”吧!

成都創(chuàng)新互聯(lián)公司長期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為衡東企業(yè)提供專業(yè)的做網(wǎng)站、成都網(wǎng)站建設(shè),衡東網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

mysql count是一個聚合函數(shù),用于返回指定匹配條件的行數(shù);count函數(shù)的使用語法如“select count(*) from user;”,表示統(tǒng)計所有的記錄,包括NULL。

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 的記錄。

  • 如果這個字段是定義為not null的話,一行行地從記錄里面讀出這個字段,判斷不能為null,按行累加。

  • 如果這個字段定義允許為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.

mysql count的作用是什么

到此,相信大家對“mysql count的作用是什么”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

分享題目:mysqlcount的作用是什么
網(wǎng)頁地址:http://bm7419.com/article28/igiccp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計網(wǎng)站維護(hù)、軟件開發(fā)、域名注冊網(wǎng)站建設(shè)、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)