← 返回首页
MySQL基础教程(十四)
发表时间:2020-03-17 12:54:24
讲解MySQL常用内置函数

mysql内置函数列表可以从mysql官方文档查询,这里仅分类介绍些最常用到的函数。

mysql内置函数官方文档

1.关于时间的内置函数

函数 结果
CURDATE() 2019-03-01
CURRENT_DATE() 2019-03-01
UTC_DATE() 2019-03-01
CURRENT_TIME() 13:46:37
CURRENT_TIMESTAMP() 2019-03-01 13:48:30
LOCALTIME() 2019-03-01 13:48:30
SYSDATE() 2019-03-01 13:48:30
DATEDIFF('2016-01-02','2016-01-01') 1

以上函数,可以通过以下sql语句测试:

select sysdate() from dual;

2.数学函数

函数 结果
abs(-10) 10
mod(10,-3) 1
pi() 3.141593
sqrt(25) 5
rand() 0.9057697559760601 ,0-1之间的随机数
round(3.141593,2) 3.14
pow(2,10) 1024

3.字符串函数

函数 结果
char_length('hello') 5,返回字符个数
select length('b站') from dual; 4,返回字节个数,一个汉字的utf8字符占三个字节
select CONCAT('hello,','world') from dual; hello,world
select trim(' hello, world ') from dual; hello,world ,去除两端空格
select SUBSTRING('this is a python code.',10,7) from dual; python
select REPLACE('this is a python code.','python','java') from dual; this is a java code.
select upper('this is a python code.') from dual; HIS IS A PYTHON CODE.
select lower('This Is A Python Code.') from dual; this is a python code.