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)

三、字符串函数

四、数学函数

五、其他函数

相关推荐
小Mie不吃饭2 小时前
2025 Oracle小白零基础到入门的学习路线
数据库·oracle
计算机学姐2 小时前
基于SpringBoot的送货上门系统【2026最新】
java·vue.js·spring boot·后端·mysql·spring·tomcat
麒qiqi2 小时前
SQLite3 数据库
数据库·oracle
码农水水2 小时前
国家电网Java面试被问:二叉树的前序、中序、后序遍历
java·开发语言·面试
不吃橘子的橘猫2 小时前
NVIDIA DLI 《Build a Deep Research Agent》学习笔记
开发语言·数据库·笔记·python·学习·算法·ai
程序 代码狂人2 小时前
DML,DDL,DCL,TCL
数据库
Xの哲學2 小时前
Linux CFS 调度器深度解析
linux·服务器·算法·架构·边缘计算
大聪明-PLUS2 小时前
关于 systemd 和桌面应用程序自动启动
linux·嵌入式·arm·smarc
逻极2 小时前
FastAPI + SQLAlchemy 现代API项目实战:从零到上手的Python MySQL开发指南
python·mysql·fastapi·异步·sqlalchemy