MariaDB中COUNT()函数的作用及用法技巧是什么
Admin 2022-06-28 群英技术资讯 1023 次浏览
这篇文章主要介绍了MariaDB中COUNT()函数的作用及用法技巧是什么相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇MariaDB中COUNT()函数的作用及用法技巧是什么文章都会有所收获,下面我们一起来看看吧。在MariaDB数据库中,COUNT()函数用于返回表达式的计数/行数。
语法:
SELECT COUNT(aggregate_expression)
FROM tables
[WHERE conditions];
注:
COUNT()函数只计算NOT NULL值。
示例:
假设有一个students表,有以下数据:
MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
| 1 | Maxsu | Haikou | 2017-01-07 |
| 3 | JMaster | Beijing | 2016-05-07 |
| 4 | Mahesh | Guangzhou | 2016-06-07 |
| 5 | Kobe | Shanghai | 2016-02-07 |
| 6 | Blaba | Shengzhen | 2016-08-07 |
| 7 | Maxsu | Sanya | 2017-08-08 |
| 8 | Maxsu | Haikou | 2015-11-17 |
+------------+--------------+-----------------+----------------+
7 rows in set (0.00 sec)
从students表中统计student_id:
SELECT COUNT(student_id) FROM Students;
-- 或者
SELECT COUNT(*) FROM Students;
执行上面查询语句,得到以下结果 -
MariaDB [testdb]> SELECT COUNT(student_id) FROM Students;
+-------------------+
| COUNT(student_id) |
+-------------------+
| 7 |
+-------------------+
1 row in set (0.07 sec)
统计student_name是Maxsu或Kobe的学生人数。参考以下查询语句 -
SELECT COUNT(*) AS "Number of Students"
FROM Students
WHERE student_name in ('Maxsu', 'Kobe');
执行上面查询语句,得到以下结果 -
MariaDB [testdb]> SELECT COUNT(*) AS "Number of Students"
-> FROM Students
-> WHERE student_name in ('Maxsu', 'Kobe');
+--------------------+
| Number of Students |
+--------------------+
| 4 |
+--------------------+
1 row in set (0.00 sec)
DISTINCT子句与COUNT()函数一起使用以防止重复计数。它只包含原始记录。
SELECT COUNT(DISTINCT student_name) AS "Number of Unique names"
FROM Students
WHERE student_name in ('Maxsu', 'Kobe');
执行上面查询语句,得到以下结果 -
MariaDB [testdb]> SELECT COUNT(DISTINCT student_name) AS "Number of Unique names"
-> FROM Students
-> WHERE student_name in ('Maxsu', 'Kobe');
+------------------------+
| Number of Unique names |
+------------------------+
| 2 |
+------------------------+
1 row in set (0.08 sec)
从查询结果中可以看到,比上一个示例少了两行。
为了更好地演示COUNT()函数对NULL值的处理,这里再插入两条记录 -
-- 修改表字段接受NULL默认值
ALTER TABLE students CHANGE student_address student_address varchar(32) default NULL;
-- 插入第1行
INSERT INTO students
(student_name, student_address, admission_date)
VALUES('Himin',NULL,'2017-01-07 00:00:00');
-- 插入第2行
INSERT INTO students
(student_name, student_address, admission_date)
VALUES('Hiavg',NULL,NULL);
执行上面查询语句,得到以下结果 -
MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
| 1 | Maxsu | Haikou | 2017-01-07 |
| 3 | JMaster | Beijing | 2016-05-07 |
| 4 | Mahesh | Guangzhou | 2016-06-07 |
| 5 | Kobe | Shanghai | 2016-02-07 |
| 6 | Blaba | Shengzhen | 2016-08-07 |
| 7 | Maxsu | Sanya | 2017-08-08 |
| 8 | Maxsu | Haikou | 2015-11-17 |
| 9 | Himin | NULL | 2017-01-07 |
| 10 | Hiavg | NULL | NULL |
+------------+--------------+-----------------+----------------+
9 rows in set (0.00 sec)
现在来看看使用count()函数来测试对NULL值的计算效果。
select count(student_address) from students;
执行上面查询语句,得到以下结果 -
MariaDB [testdb]> select count(student_address) from students;
+------------------------+
| count(student_address) |
+------------------------+
| 7 |
+------------------------+
1 row in set (0.00 sec)
可以看到,COUNT(student_address)函数它并没有统计包含NULL值的行。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章给大家分享的是MariaDB建立连接的方法。与MariaDB建立连接的方法是在命令提示符下执行mysql小编觉得挺实用的,文中的介绍很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
MariaDB安装的操作步骤及测试方法是什么? MariaDB的所有下载都位于https://downloads.mariadb.org/。从下载列表中单击指向所需版本的链接即可完成下载。
在MariaDB数据库中,UNION运算符用于组合两个或更多SELECT语句的结果集。它删除各种SELECT语句之间的重复行。
这篇文章给大家分享的是MariaDB中order by子句的相关内容。ORDER BY子句是对查询的结果进行排序,小编觉得挺实用的,因此分享给大家做个参考,文中介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
在本章中,我们将学习如何创建表。 下文有实例供大家参考,对大家了解操作过程或相关知识有一定的帮助,而且实用性强,希望这篇文章能帮助大家,下面我们一起来了解看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008