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)

三、字符串函数

四、数学函数

五、其他函数

相关推荐
Lee川16 小时前
优雅进化的JavaScript:从ES6+新特性看现代前端开发范式
javascript·面试
倔强的石头_16 小时前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
Lee川19 小时前
从异步迷雾到优雅流程:JavaScript异步编程与内存管理的现代化之旅
javascript·面试
晴殇i21 小时前
揭秘JavaScript中那些“不冒泡”的DOM事件
前端·javascript·面试
chlk12321 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
绝无仅有21 小时前
Redis过期删除与内存淘汰策略详解
后端·面试·架构
绝无仅有1 天前
Redis大Key问题排查与解决方案全解析
后端·面试·架构
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
AAA梅狸猫1 天前
Looper.loop() 循环机制
面试