MySQL内置函数

前言

        hello,各位小伙伴,大家好!本章文章为大家介绍MySQL中提供的内置函数,了解了这些内置函数之后,在以后数据库中对数据的操作会更加游刃有余。

MySQL内置函数_第1张图片

 接下来,就让我们一起来看看吧!

1.日期函数

如下表所示:

MySQL内置函数_第2张图片

 a.获取年月日

mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-07-02     |
+----------------+
1 row in set (0.00 sec)


b.获取时分秒

mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 11:03:37       |
+----------------+
1 row in set (0.00 sec)

c.获得时间戳

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-07-02 11:04:54 |
+---------------------+
1 row in set (0.00 sec)

注:在MySQL中时间戳是转换为年月日时分秒显示的

d.在日期的基础上加日期:

--在日期的基础上加上10天
mysql> select date_add('2023-7-02',interval 10 day);
+---------------------------------------+
| date_add('2023-7-02',interval 10 day) |
+---------------------------------------+
| 2023-07-12                            |
+---------------------------------------+
1 row in set (0.00 sec)

e.在日期的基础上减去日期

mysql> select date_sub('2023-07-02',interval 10 day);
+----------------------------------------+
| date_sub('2023-07-02',interval 10 day) |
+----------------------------------------+
| 2023-06-22                             |
+----------------------------------------+
1 row in set (0.00 sec)

f.计算两个日期之间相差多少天

mysql> select datediff('2022-3-31','2023-7-02');
+-----------------------------------+
| datediff('2022-3-31','2023-7-02') |
+-----------------------------------+
|                              -458 |
+-----------------------------------+
1 row in set (0.00 sec)

g.获取当前日期的年月日和时分秒

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2023-07-02 11:12:51 |
+---------------------+
1 row in set (0.00 sec)

上面介绍了每种函数的功能和使用方法,下面我们就一起来看看在表中是如何使用的:

案例:

创建一张表,记录生日

mysql> create table if not exists tmp( id int primary key auto_increment, birthday date );
Query OK, 0 rows affected (0.03 sec)

添加当前日期

mysql> insert into tmp(birthday) values(current_date());
Query OK, 1 row affected (0.01 sec)

mysql> select* from tmp;
+----+------------+
| id | birthday   |
+----+------------+
|  1 | 2023-07-02 |
+----+------------+
1 row in set (0.00 sec)

案例:

创建一个留言表

mysql> create table if not exists msg(
    -> id int primary key auto_increment,
    -> content varchar(30) not null,
    -> sendtime datetime
    -> );
Query OK, 0 rows affected (0.04 sec)

插入数据

mysql> insert into msg (content,sendtime)values('每天进步一点点,坚持带来大改变',now());
Query OK, 1 row affected (0.01 sec)

mysql> insert into msg (content,sendtime)values('纸上得来终觉浅,绝知此事要躬行',now());
Query OK, 1 row affected (0.01 sec)
mysql> select* from msg;
+----+-----------------------------------------------+---------------------+
| id | content                                       | sendtime            |
+----+-----------------------------------------------+---------------------+
|  1 | 每天进步一点点,坚持带来大改变                  | 2023-07-02 11:24:12 |
|  2 | 纸上得来终觉浅,绝知此事要躬行                  | 2023-07-02 11:24:46 |
+----+-----------------------------------------------+---------------------+
2 rows in set (0.00 sec)

显示所有留言信息,发布日期只显示日期,不用显示时间

mysql> select content,date(sendtime) from msg;
+-----------------------------------------------+----------------+
| content                                       | date(sendtime) |
+-----------------------------------------------+----------------+
| 每天进步一点点,坚持带来大改变                   | 2023-07-02     |
| 纸上得来终觉浅,绝知此事要躬行                   | 2023-07-02     |
+-----------------------------------------------+----------------+
2 rows in set (0.00 sec)

请查询在2分钟内发布的帖子

实现思路:如图所示

MySQL内置函数_第3张图片

mysql> select* from msg where date_add(sendtime,interval 2 minute) > now();
+----+-----------------------+---------------------+
| id | content               | sendtime            |
+----+-----------------------+---------------------+
|  3 | 天生我材必有用        | 2023-07-02 11:34:36 |
|  4 | 千金散尽还复来        | 2023-07-02 11:35:12 |
+----+-----------------------+---------------------+
2 rows in set (0.00 sec)

2.字符串函数

如下表所示:

MySQL内置函数_第4张图片

 案例

a.获取emp表的ename列的字符集

mysql> select charset(ename) from emp;
+----------------+
| charset(ename) |
+----------------+
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
| utf8           |
+----------------+
14 rows in set (0.00 sec)

b.要求显示exam_result表中的信息,显示格式:“XXX的语文是XXX分,数学XXX分,英语XXX分”

mysql> select concat(name,'的语文是:',chinese,'分,数学是:',math,'分,英语是:',english,'分') '分数' from exam_result;
+-----------------------------------------------------------------------+
| 分数                                                                  |
+-----------------------------------------------------------------------+
| 唐三藏的语文是:67分,数学是:98分,英语是:56分                      |
| 孙悟空的语文是:87分,数学是:78分,英语是:77分                      |
| 猪悟能的语文是:88分,数学是:98分,英语是:90分                      |
| 曹孟德的语文是:82分,数学是:84分,英语是:67分                      |
| 刘玄德的语文是:55分,数学是:85分,英语是:45分                      |
| 孙权的语文是:70分,数学是:73分,英语是:78分                        |
| 宋公明的语文是:75分,数学是:65分,英语是:30分                      |
+-----------------------------------------------------------------------+
7 rows in set (0.00 sec)

c.求学生表中学生姓名占用的字节数


mysql> select name,length(name) 字节 from student;
+--------+--------+
| name   | 字节   |
+--------+--------+
| 张三   |      6 |
| 李四   |      6 |
| 王五   |      6 |
| 张飞   |      6 |
| 刘备   |      6 |
+--------+--------+
5 rows in set (0.00 sec)

注意:length函数返回字符串长度,以字节为单位。如果是多字节字符则计算多个字节数;如果是单字节字符则算作一个字节。比如:字母,数字算作一个字节,中文表示多个字节数(与字符集编码有关)

d.将EMP表中所有名字中有S的替换成'上海'

mysql> select replace(ename,'S','上海') ename from emp;
+------------+
| ename      |
+------------+
| 上海MITH   |
| ALLEN      |
| WARD       |
| JONE上海   |
| MARTIN     |
| BLAKE      |
| CLARK      |
| 上海COTT   |
| KING       |
| TURNER     |
| ADAM上海   |
| JAME上海   |
| FORD       |
| MILLER     |
+------------+
14 rows in set (0.00 sec)

e.截取EMP表中ename字段的第二个到第三个字符

mysql> select substring(ename,2,2),ename from emp;
+----------------------+--------+
| substring(ename,2,2) | ename  |
+----------------------+--------+
| MI                   | SMITH  |
| LL                   | ALLEN  |
| AR                   | WARD   |
| ON                   | JONES  |
| AR                   | MARTIN |
| LA                   | BLAKE  |
| LA                   | CLARK  |
| CO                   | SCOTT  |
| IN                   | KING   |
| UR                   | TURNER |
| DA                   | ADAMS  |
| AM                   | JAMES  |
| OR                   | FORD   |
| IL                   | MILLER |
+----------------------+--------+
14 rows in set (0.00 sec)

f.以首字母小写的方式显示所有员工的姓名

mysql> select concat(lcase(substring(ename,1,1)),substring(ename,2)), ename from emp;
+--------------------------------------------------------+--------+
| concat(lcase(substring(ename,1,1)),substring(ename,2)) | ename  |
+--------------------------------------------------------+--------+
| sMITH                                                  | SMITH  |
| aLLEN                                                  | ALLEN  |
| wARD                                                   | WARD   |
| jONES                                                  | JONES  |
| mARTIN                                                 | MARTIN |
| bLAKE                                                  | BLAKE  |
| cLARK                                                  | CLARK  |
| sCOTT                                                  | SCOTT  |
| kING                                                   | KING   |
| tURNER                                                 | TURNER |
| aDAMS                                                  | ADAMS  |
| jAMES                                                  | JAMES  |
| fORD                                                   | FORD   |
| mILLER                                                 | MILLER |
+--------------------------------------------------------+--------+
14 rows in set (0.00 sec)

3.数学函数

如下表所示:

MySQL内置函数_第5张图片

绝对值:

mysql> select abs(-20.3);
+------------+
| abs(-20.3) |
+------------+
|       20.3 |
+------------+
1 row in set (0.00 sec)

十进制转换为二进制:

mysql> select bin(10);
+---------+
| bin(10) |
+---------+
| 1010    |
+---------+
1 row in set (0.00 sec)

转换成16进制:

mysql> select hex(15);
+---------+
| hex(15) |
+---------+
| F       |
+---------+
1 row in set (0.00 sec)

进制转换:

--将数字10由10进制转换为2进制
mysql> select conv(10,10,2);
+---------------+
| conv(10,10,2) |
+---------------+
| 1010          |
+---------------+
1 row in set (0.00 sec)

向上取整:

mysql> select ceiling(3.14);
+---------------+
| ceiling(3.14) |
+---------------+
|             4 |
+---------------+
1 row in set (0.00 sec)

mysql> select ceiling(-3.14);
+----------------+
| ceiling(-3.14) |
+----------------+
|             -3 |
+----------------+
1 row in set (0.00 sec)

向下取整:


mysql> select floor(3.14);
+-------------+
| floor(3.14) |
+-------------+
|           3 |
+-------------+
1 row in set (0.00 sec)

mysql> select floor(-3.14);
+--------------+
| floor(-3.14) |
+--------------+
|           -4 |
+--------------+
1 row in set (0.00 sec)

保留两位小数位数:

mysql> select format(3.1415926535,2);
+------------------------+
| format(3.1415926535,2) |
+------------------------+
| 3.14                   |
+------------------------+
1 row in set (0.00 sec)

产生随机数:范围[0.0,1.0)

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.8258876640590724 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.9942531697398547 |
+--------------------+
1 row in set (0.00 sec)

4.其它函数

user() 查询当前用户

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

md5(str)对一个字符串进行md5摘要,摘要后得到一个32位字符串

mysql> select md5('hello');
+----------------------------------+
| md5('hello')                     |
+----------------------------------+
| 5d41402abc4b2a76b9719d911017c592 |
+----------------------------------+
1 row in set (0.01 sec)

database()显示当前正在使用的数据库

mysql> select database();
+------------+
| database() |
+------------+
| scott      |
+------------+
1 row in set (0.00 sec)

password()函数,MySQL数据库使用该函数对用户加密

mysql> select password('hellow');
+-------------------------------------------+
| password('hellow')                        |
+-------------------------------------------+
| *D04430B5DD8059E94D70AA54A6CD32D3019E5771 |
+-------------------------------------------+
1 row in set, 1 warning (0.02 sec)

ifnull(val1, val2) 如果val1为null,返回val2,否则返回val1的值

mysql> select ifnull(10,20);
+---------------+
| ifnull(10,20) |
+---------------+
|            10 |
+---------------+
1 row in set (0.00 sec)

mysql> select ifnull(null,20);
+-----------------+
| ifnull(null,20) |
+-----------------+
|              20 |
+-----------------+
1 row in set (0.00 sec)

总结

        本篇文章为大家详细介绍了MySQL中的常用内置函数,掌握了这些内置函数之后,在以后的数据库中对数据的操作可以更加方便,实现更多的需求。

MySQL内置函数_第6张图片

 我们下次再见!

你可能感兴趣的:(mysql,数据库)