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)

三、字符串函数

四、数学函数

五、其他函数

相关推荐
慾玄12 小时前
第一次渗透
linux
a努力。12 小时前
宇树Java面试被问:数据库死锁检测和自动回滚机制
java·数据库·elasticsearch·面试·职场和发展·rpc·jenkins
数据知道12 小时前
PostgreSQL实战:详解权限设置与管理全流程
数据库·postgresql
物理与数学13 小时前
Linux 内核 TLB 优化
linux·linux内核
Dontla13 小时前
Database Schema Introduction (structure of data, NoSQL schema)
数据库·nosql
啟明起鸣13 小时前
【Linux 项目管理工具】GDB 调试是现成 C/C++ 项目的 “造影剂”,用来分析项目的架构原理
linux·c语言·c++
2401_8322981013 小时前
存算分离2.0,阿里云EMR Serverless破解数据处理瓶颈
数据库
物理与数学13 小时前
linux 交换分区(Swap)
linux·linux内核
Maggie_ssss_supp13 小时前
Linux-MySQL日志管理
数据库·mysql
熬夜敲代码的小N13 小时前
MySQL数据可视化实战:从SQL雕琢到图表绽放
sql·mysql·信息可视化