以下是阿鲤对mysql内置函数学习的总结,希望可以帮助到大家;如果你不了解mysql,可以先看一下阿鲤的另两篇博客mysql基础 和 mysql表操作

一:日期函数

二:聚合函数

三:字符串函数


一:日期函数

函数名称

描述

current_date()

当亲日期

current_time()

当前时间

current_timetamp()

当前时间戳

date(datetime)

返回datetime参数的日期部分

date_add(date, interval d_value_type)

在date中添加日期或事件,interval后面的数值可以是:year,minute,second,day

date_sub(date, interval d_value_type)

在date中减去日期或事件,interval后面的数值可以是:year,minute,second,day

datediff(date1, date2)

两个日期的差值,单位是天

now()

当前日期时间

eg:

1:获取当前日期

MariaDB [(none)]> select current_date();
+----------------+
| current_date() |
+----------------+
| 2020-06-26     |
+----------------+
1 row in set (0.00 sec)

2:获取当前时间

MariaDB [(none)]> select current_time;
+--------------+
| current_time |
+--------------+
| 13:59:33     |
+--------------+
1 row in set (0.00 sec)

3:获得时间戳

MariaDB [(none)]> select current_timestamp;
+---------------------+
| current_timestamp   |
+---------------------+
| 2020-06-26 14:00:08 |
+---------------------+
1 row in set (0.00 sec)

4:在日期的基础上加上时间

MariaDB [(none)]> select date_add('2021-6-27', interval 10 day);
+----------------------------------------+
| date_add('2021-6-27', interval 10 day) |
+----------------------------------------+
| 2021-07-07                             |
+----------------------------------------+
1 row in set (0.00 sec)

5:在日期的基础上减去时间

MariaDB [(none)]> select date_sub('2021-6-27', interval 10 day);
+----------------------------------------+
| date_sub('2021-6-27', interval 10 day) |
+----------------------------------------+
| 2021-06-17                             |
+----------------------------------------+
1 row in set (0.00 sec)

6:计算两个日期之间相差多少天

MariaDB [(none)]> select datediff('2021-6-27','2021-3-27');
+-----------------------------------+
| datediff('2021-6-27','2021-3-27') |
+-----------------------------------+
|                                92 |
+-----------------------------------+
1 row in set (0.00 sec)

7:获取当前日期时间

MariaDB [(none)]> select now();
+---------------------+
| now()               |
+---------------------+
| 2020-06-26 14:04:21 |
+---------------------+
1 row in set (0.00 sec)

8:创建一张表,记录生日

MariaDB [db1]> create table name_birthday( id int primary key auto_increment,         
               birthday date, 
                );
Query OK, 0 rows affected (0.01 sec)

//插入当前日期
insert into table name_birthday(birthday) values(current_date()); 
mysql> select * from table name_birthday; 
+----+------------+ 
| id | birthday   | 
+----+------------+ 
|  1 | 2017-11-19 | 
+----+------------+

9:创建一个留言表

//创建
MariaDB [db1]> create table msg(
    -> id int primary key auto_increment,
    -> content varchar(30) not null,
    -> sendtime datetime
    -> );
Query OK, 0 rows affected (0.01 sec)

//插入
MariaDB [db1]> insert into msg(content,sendtime) values('hello1', now());
Query OK, 1 row affected (0.00 sec)

MariaDB [db1]> insert into msg(content,sendtime) values('hello2', now());
Query OK, 1 row affected (0.00 sec)

//查看
MariaDB [db1]> select *from msg;
+----+---------+---------------------+
| id | content | sendtime            |
+----+---------+---------------------+
|  1 | hello1  | 2020-06-26 14:23:06 |
|  2 | hello2  | 2020-06-26 14:23:12 |
+----+---------+---------------------+
2 rows in set (0.00 sec)

//显示所有留言信息,发布日期只显示日期,不用显示时间
MariaDB [db1]> select content,date(sendtime) from msg;
+---------+----------------+
| content | date(sendtime) |
+---------+----------------+
| hello1  | 2020-06-26     |
| hello2  | 2020-06-26     |
+---------+----------------+
2 rows in set (0.00 sec)


//查询在两分钟内发布的帖子
MariaDB [db1]> select * from msg where date_add(sendtime, interval 2 minute) > now();
Empty set (0.00 sec)

二:聚合函数

函数名称

说明

count([distinct] exrp)

返回查询到数据的数量

sum([distinct] exrp)   

返回查询到数据的总和,不是数字无意义

avg([distinct] exrp)

返回查询到数据的平均值,不是数字无意义

max([distinct] exrp)

返回查询到数据的最大值,不是数字无意义

min([distinct] exrp)

返回查询到数据的最小值,不是数组无意义

eg:

首先创建一个表格

//创建表结构 
CREATE TABLE exam_result ( 
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, 
name VARCHAR(20) NOT NULL COMMENT '同学姓名', 
yuwen float DEFAULT 0.0 COMMENT '语文成绩', 
shuxue float DEFAULT 0.0 COMMENT '数学成绩', 
yingyu float DEFAULT 0.0 COMMENT '英语成绩' 
);
 
//插入测试数据 
INSERT INTO exam_result (name, yuwen, shuxue, yingyu) VALUES 
('唐三藏', 67, 98, 56), 
('孙悟空', 87, 78, 77), 
('猪悟能', 88, 98, 90), 
('曹孟德', 82, 84, 67), 
('刘玄德', 55, 85, 45), 
('孙权', 70, 73, 78), 
('宋公明', 75, 65, 30); 
Query OK, 
7 rows affected (0.00 sec) Record
s: 7 Duplicates: 0 Warnings: 0

统计参加考试的有多少人

MariaDB [db2]> select count(*) from exam_result;
+----------+
| count(*) |
+----------+
|        7 |
+----------+
1 row in set (0.00 sec)

统计本次考试的数学成绩分数个数

//去重
MariaDB [db2]> select count(distinct shuxue) from exam_result;
+------------------------+
| count(distinct shuxue) |
+------------------------+
|                      6 |
+------------------------+
1 row in set (0.00 sec)

//不去重
MariaDB [db2]> select count(shuxue) from exam_result;
+---------------+
| count(shuxue) |
+---------------+
|             7 |
+---------------+
1 row in set (0.00 sec)

统计数学成绩总分

MariaDB [db2]> select sum(shuxue) from exam_result;
+-------------+
| sum(shuxue) |
+-------------+
|         581 |
+-------------+
1 row in set (0.00 sec)

 统计平均总分

MariaDB [db2]> select avg(yuwen + shuxue + yingyu) average_total from exam_result;
+--------------------+
| average_total      |
+--------------------+
| 221.14285714285714 |
+--------------------+
1 row in set (0.00 sec)

三:字符串函数

函数名称

描述

charset(str)

返回字符串字符集

concat(string2 [, ...])

链接字符串

instr(string, substring)

返回substring在string中出现的位置,没有返回0

ucase(string2)

转化成大写

lcase(string2)

转化成小写

left(string2, length)

从string2中的左边起取length个字符

length(string)

string的长度

replace(str, search_str, replace_str)

在str中用replace_str替换search_str

strcmp(string1, string2)

逐字符比较两个字符串大小

substring(str, position, [,length])

从str的postio开始,取出length个字符

ltrim(string)  rtrim(string)  trim(string)

去除前空格或后空格

....持续更新中。