内置函数【MySQL】
文章目录
- 日期函数
- 字符串函数
- 数学函数
- 其他函数
日期函数
current_date函数用于获取当前的日期
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-11-03 |
+----------------+
1 row in set (0.00 sec)
current_time函数用于获取当前的时间
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 19:34:25 |
+----------------+
1 row in set (0.00 sec)
current_timestamp函数用于获取当前的时间戳,以日期时间格式进行显示
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-11-03 19:34:51 |
+---------------------+
1 row in set (0.00 sec)
now函数用于获取当前的日期时间
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2024-11-03 19:35:12 |
+---------------------+
1 row in set (0.00 sec)
date函数用于获取当前的日期时间
mysql> select date(now() );
+--------------+
| date(now() ) |
+--------------+
| 2024-11-03 |
+--------------+
1 row in set (0.00 sec)
date_add函数用于在日期的基础上添加日期或时间
mysql> select date_add(current_date() , interval 10 day );
+----------------------------------------------+
| date_add(current_date() , interval 10 day ) |
+----------------------------------------------+
| 2024-11-13 |
+----------------------------------------------+
1 row in set (0.00 sec)
如果在date_add函数中添加的日期/时间为负值,则相当于在日期的基础上减去日期/时间
mysql> select date_add(current_date() , interval -10 day );
+-----------------------------------------------+
| date_add(current_date() , interval -10 day ) |
+-----------------------------------------------+
| 2024-10-24 |
+-----------------------------------------------+
1 row in set (0.00 sec)
date_sub函数用于在日期的基础上减去日期或时间
如果在date_sub函数中减去的日期/时间为负值,则相当于在日期的基础上添加日期/时间
mysql> select date_sub(current_date() , interval 10 day );
+----------------------------------------------+
| date_sub(current_date() , interval 10 day ) |
+----------------------------------------------+
| 2024-10-24 |
+----------------------------------------------+
1 row in set (0.00 sec)mysql> select date_sub(current_date() , interval -10 day );
+-----------------------------------------------+
| date_sub(current_date() , interval -10 day ) |
+-----------------------------------------------+
| 2024-11-13 |
+-----------------------------------------------+
1 row in set (0.00 sec)
datediff函数用于获取两个日期的差,单位是天
mysql> select datediff(current_date() , '2021-01-01' );
+-------------------------------------------+
| datediff(current_date() , '2021-01-01' ) |
+-------------------------------------------+
| 1402 |
+-------------------------------------------+
1 row in set (0.00 sec)
创建一个留言表,表中包含自增长的主键id、昵称、评论内容和评论时间
mysql> create table comment_form ( id int primary key auto_increment,-> nickname varchar(20) not null ,-> content varchar(50) not null ,-> sendtime datetime-> );
Query OK, 0 rows affected (0.02 sec)mysql> desc comment_form ;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| nickname | varchar(20) | NO | | NULL | |
| content | varchar(50) | NO | | NULL | |
| sendtime | datetime | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
插入数据 , 插入时直接通过now函数指明评论时间
mysql> insert into comment_form (nickname ,content,sendtime) values('张三', 'hello1', now());
Query OK, 1 row affected (0.00 sec)mysql> insert into comment_form (nickname ,content,sendtime) values('李四', 'hello1', now());
Query OK, 1 row affected (0.00 sec)mysql> select * from comment_form ;
+----+----------+---------+---------------------+
| id | nickname | content | sendtime |
+----+----------+---------+---------------------+
| 1 | 张三 | hello1 | 2024-11-03 21:33:14 |
| 2 | 李四 | hello1 | 2024-11-03 21:33:19 |
+----+----------+---------+---------------------+
2 rows in set (0.00 sec)
如果只想显示评论的日期而不显示评论的时间,可以在查询sendtime字段时,通过date函数截取sendtime的日期部分进行显示
mysql> select nickname,content,date(sendtime) from comment_form ;
+----------+---------+----------------+
| nickname | content | date(sendtime) |
+----------+---------+----------------+
| 张三 | hello1 | 2024-11-03 |
| 李四 | hello1 | 2024-11-03 |
+----------+---------+----------------+
2 rows in set (0.00 sec)
向表中插入一些数据 , 并查询2分钟内发布的评论信息,实际就是要筛选出评论时间加上2分钟后大于当前时间的评论,这时需要同时借助date_add和now函数
ysql> insert into comment_form (nickname ,content,sendtime) values('李逵', 'hello1', now());
Query OK, 1 row affected (0.00 sec)mysql> select * from comment_form where date_add(sendtime , interval 2 minute)> now() ;
+----+----------+---------+---------------------+
| id | nickname | content | sendtime |
+----+----------+---------+---------------------+
| 6 | 李逵 | hello1 | 2024-11-03 22:19:54 |
+----+----------+---------+---------------------+
1 row in set (0.00 sec)
字符串函数
charset,charset函数获取ename列使用的字符集即可
获取员工表中ename列使用的字符集
mysql> select * from emp;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
| 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 |
| 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 |
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
| 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 |
| 007782 | CLARK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
| 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
| 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 |
| 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
| 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
| 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.00 sec)mysql> select ename,charset(ename) from emp;
+--------+----------------+
| ename | charset(ename) |
+--------+----------------+
| SMITH | utf8 |
| ALLEN | utf8 |
| WARD | utf8 |
| JONES | utf8 |
| MARTIN | utf8 |
| BLAKE | utf8 |
| CLARK | utf8 |
| SCOTT | utf8 |
| KING | utf8 |
| TURNER | utf8 |
| ADAMS | utf8 |
| JAMES | utf8 |
| FORD | utf8 |
| MILLER | utf8 |
+--------+----------------+
14 rows in set (0.00 sec)
concat ,查询成绩表中的信息时,使用concat函数按要求进行字符串连接
mysql> select concat('a','b','c');
+---------------------+
| concat('a','b','c') |
+---------------------+
| abc |
+---------------------+
1 row in set (0.00 sec)
例如:成绩表,要求以“XXX的语文是XX分,数学是XX分,英语是XX分”的格式显示成绩表中的信息
mysql> select concat (name ,'的语文是' ,chinese,'分,数学是' ,math,'分,英语是',english,'分') 成绩概览 from exam_result;
+-----------------------------------------------------------+
| 成绩概览 |
+-----------------------------------------------------------+
| 唐三藏的语文是67分,数学是98分,英语是56分 |
| 猪悟能的语文是88分,数学是98分,英语是90分 |
| 曹孟德的语文是70分,数学是90分,英语是67分 |
| 刘玄德的语文是55分,数学是115分,英语是45分 |
| 孙权的语文是70分,数学是73分,英语是78分 |
+-----------------------------------------------------------+
5 rows in set (0.00 sec)
instr函数用于获取一个字符串在另一个字符串中首次出现的位置,如果没有出现则返回0
mysql> select instr ('2024zhangsan', 'zhangsan') ;
+------------------------------------+
| instr ('2024zhangsan', 'zhangsan') |
+------------------------------------+
| 5 |
+------------------------------------+
1 row in set (0.00 sec)
ucase,用于获取转换成大写后的字符串
lcase函数用于获取转换成小写后的字符串
mysql> select ucase('abcd') ;
+---------------+
| ucase('abcd') |
+---------------+
| ABCD |
+---------------+
1 row in set (0.00 sec)
mysql> select lcase('ABC') ;
+--------------+
| lcase('ABC') |
+--------------+
| abc |
+--------------+
1 row in set (0.00 sec)
left函数用于从字符串的左边开始,向后截取指定个数的字符
mysql> select left('123456789' ,2) ;
+----------------------+
| left('123456789' ,2) |
+----------------------+
| 12 |
+----------------------+
1 row in set (0.00 sec)
length函数用于获取字符串占用的字节数
mysql> select name ,length(name) from exam_result ;
+-----------+--------------+
| name | length(name) |
+-----------+--------------+
| 唐三藏 | 9 |
| 猪悟能 | 9 |
| 曹孟德 | 9 |
| 刘玄德 | 9 |
| 孙权 | 6 |
+-----------+--------------+
5 rows in set (0.00 sec)
对于多字节字符来说,不同编码中一个字符所占的字节个数是不同的,比如utf8中一个字符占用3个字节,而gbk中一个字符占用2个字节。
replace函数用于将字符串中的指定子字符串替换成另一个字符串
例如:将员工表中所有名字中的“S”替换成“上海”
mysql> select ename ,replace (ename ,'S' ,'上海' ) from emp;
+--------+---------------------------------+
| ename | replace (ename ,'S' ,'上海' ) |
+--------+---------------------------------+
| SMITH | 上海MITH |
| ALLEN | ALLEN |
| WARD | WARD |
| JONES | JONE上海 |
| MARTIN | MARTIN |
| BLAKE | BLAKE |
| CLARK | CLARK |
| SCOTT | 上海COTT |
| KING | KING |
| TURNER | TURNER |
| ADAMS | ADAM上海 |
| JAMES | JAME上海 |
| FORD | FORD |
| MILLER | MILLER |
+--------+---------------------------------+
14 rows in set (0.00 sec)
strcmp函数用于逐字符按照ASCII码比较两个字符串的大小,两个字符串大小相等返回0,前者大返回1,后者大返回-1
strcmp函数在比较时是不区分大小写的
mysql> select strcmp('abcd', 'abcd') ;
+------------------------+
| strcmp('abcd', 'abcd') |
+------------------------+
| 0 |
+------------------------+
1 row in set (0.00 sec)mysql> select strcmp('abcd', 'bbcd') ;
+------------------------+
| strcmp('abcd', 'bbcd') |
+------------------------+
| -1 |
+------------------------+
1 row in set (0.00 sec)mysql> select strcmp('abcd', 'ABcd') ;
+------------------------+
| strcmp('abcd', 'ABcd') |
+------------------------+
| 0 |
+------------------------+
1 row in set (0.00 sec)
substring,从字符串的指定位置开始,向后截取指定个数的字符
mysql> select substring('123456', 2,3);
+--------------------------+
| substring('123456', 2,3) |
+--------------------------+
| 234 |
+--------------------------+
1 row in set (0.00 sec)
如果没有指定要截取的字符个数,则默认从指定位置开始截取到最后
mysql> select substring('123456', 2);
+------------------------+
| substring('123456', 2) |
+------------------------+
| 23456 |
+------------------------+
1 row in set (0.00 sec)
ltrim、rtrim和trim
trim函数用于去除字符串的前后空格
mysql> select trim(' zhangsan ') 去除结果;
+--------------+
| 去除结果 |
+--------------+
| zhangsan |
+--------------+
1 row in set (0.00 sec)
ltrim和rtrim函数分别用于去除字符串的前空格和后空格
mysql> select ltrim(' zhangsan ') 去除结果;
+------------------+
| 去除结果 |
+------------------+
| zhangsan |
+------------------+
1 row in set (0.00 sec)mysql> select rtrim(' zhangsan ') 去除结果;
+------------------+
| 去除结果 |
+------------------+
| zhangsan |
+------------------+
1 row in set (0.00 sec)
要求以首字母小写的方式显示员工表中所有员工的姓名
- 使用substring函数,截取员工姓名的第一个字符,然后使用lcase将其转换成小写。
- 使用substring函数,截取员工姓名的第二个字符及其后续字符。
- 使用concat函数,将第一次截取并转换成小写的首字母,和第二次截取的字符串进行连接。
mysql> select ename, concat ( lcase( substring(ename,1,1 )) ,substring(ename,2) ) 首字母小写 from emp;
+--------+-----------------+
| 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)
数学函数
abs函数用于获取一个数的绝对值
mysql> select abs (-1024) ;
+-------------+
| abs (-1024) |
+-------------+
| 1024 |
+-------------+
1 row in set (0.00 sec)
bin函数用于将一个十进制数转换成二进制
mysql> select bin(10) ;
+---------+
| bin(10) |
+---------+
| 1010 |
+---------+
1 row in set (0.00 sec)
hex函数用于将一个十进制数转换成十六进制
mysql> select hex(10) ;
+---------+
| hex(10) |
+---------+
| A |
+---------+
1 row in set (0.00 sec)
conv函数用于将一个数从一个进制转换成另一个进制
mysql> select conv(10,10,2) ;
+---------------+
| conv(10,10,2) |
+---------------+
| 1010 |
+---------------+
1 row in set (0.00 sec)mysql> select conv('A',16,10) ;
+-----------------+
| conv('A',16,10) |
+-----------------+
| 10 |
+-----------------+
ceiling函数用于对一个数进行向上取整
mysql> select ceiling(3.3) ;
+--------------+
| ceiling(3.3) |
+--------------+
| 4 |
+--------------+
1 row in set (0.00 sec)
向上取整本质是向正无穷方向取整,因此负数向上取整后得到的是第一个大于等于该数的整数
mysql> select ceiling(-3.3) ;
+---------------+
| ceiling(-3.3) |
+---------------+
| -3 |
+---------------+
1 row in set (0.00 sec)mysql> select ceiling(-3.8) ;
+---------------+
| ceiling(-3.8) |
+---------------+
| -3 |
+---------------+
1 row in set (0.00 sec)
floor函数用于对一个数进行向下取整
向下取整本质是向负无穷方向取整,因此负数向下取整后得到的是第一个小于等于该数的整数
mysql> select floor(3.3) ;
+------------+
| floor(3.3) |
+------------+
| 3 |
+------------+
1 row in set (0.00 sec)mysql> select floor(3.8) ;
+------------+
| floor(3.8) |
+------------+
| 3 |
+------------+
1 row in set (0.00 sec)mysql> select floor(-3.3) ;
+-------------+
| floor(-3.3) |
+-------------+
| -4 |
+-------------+
1 row in set (0.00 sec)mysql> select floor(-3.8) ;
+-------------+
| floor(-3.8) |
+-------------+
| -4 |
+-------------+
1 row in set (0.00 sec)
format函数用于对数值进行格式化,以四舍五入的方式保留指定位数的小数
mysql> select format(3.1415926,2);
+---------------------+
| format(3.1415926,2) |
+---------------------+
| 3.14 |
+---------------------+
1 row in set (0.00 sec)mysql> select format(3.1415926,3);
+---------------------+
| format(3.1415926,3) |
+---------------------+
| 3.142 |
+---------------------+
1 row in set (0.00 sec)
rand函数用于生成0.0到1.0的随机浮点数
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.9762763557734679 |
+--------------------+
1 row in set (0.00 sec)
生成0到100的随机数,可以用生成的随机浮点数乘以100,然后再以某种取整方式进行取整
mysql> select floor(rand()*100 ) ;
+--------------------+
| floor(rand()*100 ) |
+--------------------+
| 19 |
+--------------------+
1 row in set (0.00 sec)
mysql> select floor(rand()*100 ) ;
+--------------------+
| floor(rand()*100 ) |
+--------------------+
| 4 |
+--------------------+
1 row in set (0.00 sec)
mod函数用于对数值进行求余运算
mysql> select mod(10,3);
+-----------+
| mod(10,3) |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
其他函数
user函数用于获取MySQL连接的当前用户名和主机名
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
md5函数用于对一个字符串进行md5摘要,摘要后得到一个32位字符串
mysql> select md5('2024zhangsan');
+----------------------------------+
| md5('2024zhangsan') |
+----------------------------------+
| 2d658e6320d1db13f56adb9a14d094c4 |
+----------------------------------+
1 row in set (0.00 sec)
一般情况下公司内部数据库不会存储用户的明文密码,而会将用户密码形成摘要后存储对应的摘要,当用户登录账号时,将用户输入的的密码形成摘要后与数据库中存储的摘要做对比,如果对比成功则允许登录。
好处主要有两个,
第一个好处就是公司内部数据库中存储的不是用户的明文信息,就算用户信息泄露了也不会产生太大影响
第二个好处就是形成的摘要是定长的,这样有利于数据库表结构的设计
database函数用于显示当前正在使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| scott |
+------------+
1 row in set (0.00 sec)
password函数用于对用户数据进行加密
mysql> select password('abc@123ABC');
+-------------------------------------------+
| password('abc@123ABC') |
+-------------------------------------------+
| *3BBE18329F4C65A7D4DB32FA3A3D3F155EC5CD8E |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
fnull函数接受两个参数,如果第一个参数不为null则返回第一个参数值,否则返回第二个参数值
mysql> select ifnull('a','b') ;
+-----------------+
| ifnull('a','b') |
+-----------------+
| a |
+-----------------+
1 row in set (0.00 sec)mysql> select ifnull('null','b') ;
+--------------------+
| ifnull('null','b') |
+--------------------+
| null |
+--------------------+
1 row in set (0.00 sec)mysql> select ifnull('null','null') ;
+-----------------------+
| ifnull('null','null') |
+-----------------------+
| null |
+-----------------------+
1 row in set (0.00 sec)