MySQL内置函数

文章目录


一、聚合函数

建立一张成绩表并且插入一些数据:

sql 复制代码
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
|  8 | Amy       |    NULL |   80 |     100 |
|  9 | 鲁智深    |      88 |   98 |    NULL |
+----+-----------+---------+------+---------+
9 rows in set (0.00 sec)
  • 统计班级共有多少学生
sql 复制代码
mysql> select count(*) from exam_result;
+----------+
| count(*) |
+----------+
|        9 |
+----------+
1 row in set (0.03 sec)
  • 统计参加本次语文考试的同学的人数

Amy没有参加语文考试

sql 复制代码
mysql> select count(chinese) from exam_result;
+----------------+
| count(chinese) |
+----------------+
|              8 |
+----------------+
1 row in set (0.00 sec)
  • 统计本次考试的数学成绩分数个数
sql 复制代码
mysql> select count(math) from exam_result;
+-------------+
| count(math) |
+-------------+
|           9 |
+-------------+
1 row in set (0.00 sec)

查看去重分数之后的个数(有3个98分)

sql 复制代码
mysql> select count(distinct math) from exam_result;
+----------------------+
| count(distinct math) |
+----------------------+
|                    7 |
+----------------------+
1 row in set (0.00 sec)
  • 统计数学成绩总分
sql 复制代码
mysql> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
|       759 |
+-----------+
1 row in set (0.00 sec)
  • 统计数学成绩平均分(向下取整了)
sql 复制代码
mysql> select floor(avg(math)) from exam_result;
+------------------+
| floor(avg(math)) |
+------------------+
|               84 |
+------------------+
1 row in set (0.00 sec)
  • 查询英语成绩最高分
sql 复制代码
mysql> select max(english) from exam_result;
+--------------+
| max(english) |
+--------------+
|          100 |
+--------------+
1 row in set (0.00 sec)
  • 查询大于 70 分以上的数学最低分
sql 复制代码
mysql> select min(math) from exam_result where math > 70;
+-----------+
| min(math) |
+-----------+
|        73 |
+-----------+
1 row in set (0.00 sec)

group by 子句的使用

二、日期函数

  • 查询当前年月日
sql 复制代码
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2025-12-29     |
+----------------+
1 row in set (0.00 sec)
  • 查询当前时分秒
sql 复制代码
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 21:59:40       |
+----------------+
1 row in set (0.00 sec)
  • 查询当前时间戳
sql 复制代码
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2025-12-29 22:00:19 |
+---------------------+
1 row in set (0.00 sec)
  • DATE(datetime) 获得datetime的日期部分
sql 复制代码
mysql> select date('2099-12-12 22:19:56');
+-----------------------------+
| date('2099-12-12 22:19:56') |
+-----------------------------+
| 2099-12-12                  |
+-----------------------------+
1 row in set (0.00 sec)

三、字符串函数

四、数学函数

五、其他函数

相关推荐
云小逸21 小时前
【nmap源码学习】 Nmap网络扫描工具深度解析:从基础参数到核心扫描逻辑
网络·数据库·学习
·云扬·21 小时前
MySQL Binlog落盘机制深度解析:性能与安全性的平衡艺术
android·mysql·adb
肉包_51121 小时前
两个数据库互锁,用全局变量互锁会偶发软件卡死
开发语言·数据库·c++
霖霖总总21 小时前
[小技巧64]深入解析 MySQL InnoDB 的 Checkpoint 机制:原理、类型与调优
数据库·mysql
Y1rong1 天前
linux之文件IO
linux
Trouvaille ~1 天前
【Linux】UDP Socket编程实战(一):Echo Server从零到一
linux·运维·服务器·网络·c++·websocket·udp
嵌入小生0071 天前
Shell | 命令、编程及Linux操作系统的基本概念
linux·运维·服务器
此刻你1 天前
常用的 SQL 语句
数据库·sql·oracle
それども1 天前
分库分表的事务问题 - 怎么实现事务
java·数据库·mysql
·云扬·1 天前
MySQL Binlog 配置指南与核心作用解析
数据库·mysql·adb