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)

三、字符串函数

四、数学函数

五、其他函数

相关推荐
li星野1 分钟前
[特殊字符] Linux/嵌入式Linux面试模拟卷
linux·运维·面试
IvorySQL6 分钟前
PostgreSQL 19 重磅新语法终于补齐这个缺口
数据库·postgresql·开源
IvorySQL13 分钟前
PostgreSQL 技术日报 (3月23日)|使用 rdtsc 减少 EXPLAIN ANALYZE 的计时开销
数据库·postgresql·开源
yhole25 分钟前
SQL中的REGEXP正则表达式使用指南
数据库·sql·正则表达式
IvorySQL30 分钟前
PostgreSQL 技术日报 (3月21日)|这些机制,可能并非 “ 理所当然 ”
数据库·postgresql·开源
m0_5180194838 分钟前
使用Seaborn绘制统计图形:更美更简单
jvm·数据库·python
卓怡学长1 小时前
m280本科生导师指导平台
java·数据库·spring·tomcat·maven·intellij-idea
大尚来也1 小时前
Serverless架构深度解析:适用场景、核心局限与破局之道
数据库
Wave8451 小时前
非阻塞按键(单击,双击,长按)
数据库
JiMoKuangXiangQu1 小时前
Linux 锁 (4) - seqlock
linux·seqlock