【MySQL】内置函数

目录

一、日期函数

current_date()与current_time()

now()

date_add()与date_sub()

datediff()

二、字符串函数

charset(str)

concat()

length()

replace()

substring()

trim()

三、数学函数

四、其他函数


一、日期函数

常见日期函数如下:

函数名称 描述
current_date() 当前日期
current_time() 当前时间
current_timestamp() 当前时间戳
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() 当前日期时间

接下来依次介绍:

current_date()与current_time()

current_date,current_time,current_timestamp分别是返回当前日期和当前时间和当前时间戳

sql 复制代码
select current_date();
sql 复制代码
select current_time();

也可以一起使用:

sql 复制代码
select current_date(),current_time;
sql 复制代码
select current_timestamp();

now()

也可以使用now函数查看当前时间

sql 复制代码
select now();

用date函数,截取日期,会忽略显示时间

也可以嵌套函数使用:

date_add()与date_sub()

date_add和date_sub进行时间计算

都有两个参数,将所计算的时间增加或者减少多少时间

sql 复制代码
select date_add('1949-10-1', interval 10 day);
sql 复制代码
select date_sub('1949-10-1', interval 10 day);

也可以嵌套函数使用:

sql 复制代码
select date_add(now(), interval 10 day);
sql 复制代码
select date_sub(now(), interval 10 day);

datediff()

datediff时间相减

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

sql 复制代码
select datediff(now(),'2000-1-1');

这实际上是前面一个的时间减去后面一个的时间的

二、字符串函数

函数名 功能描述
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的position开始,取length个字符
ltrim(string)、rtrim(string)、trim(string) 去除前空格或后空格

charset(str)

charset(str) 返回字符串的编码集

sql 复制代码
select charset(字符串);

concat()

concat拼接字符串

sql 复制代码
select concat(字符串1,字符串2,字符串3...);

也可以嵌套函数:

length()

length求字符串长度

这里求的是字节数,而不是字符的个数

replace()

replace替换字符

sql 复制代码
replace(str, search_str, replace_str);

将deptno中的deptno中的0替换为1显示

注意,这里是替换显示,并没有真正替换

substring()

substring字符串截取

sql 复制代码
substring('字符串', 起始位置, 截取长度);

截取长度如果不写,就是从起始位置截取到最后

如上就是对字符串中,第三个字符(包含第三个)截取四个长度

也可以对一个表中进行截取

分割字符串

sql 复制代码
select dname, substring(dname, 1, 2), substring(dname,3) from dept;

trim()

ltrim和rtrim和trim

去掉左边或者右边或者左边和右边的空格的函数

三、数学函数

函数名称 描述
abs(number) 绝对值函数
bin(decimal_number) 十进制转换二进制
hex(decimalNumber) 转换成十六进制
conv(number, from_base, to_base) 进制转换
ceiling(number) 向上去整
floor(number) 向下去整
format(number, decimal_places) 格式化,保留小数位数
rand() 返回随机浮点数,范围[0.0, 1.0]
mod(number, denominator) 取模,求余

abs取绝对值

bin,十进制转二进制

conv将一个数字从某个进制转化为另一个进制

sql 复制代码
conv(num,x,y);

将num从x进制转化为y进制

ceiling和floor取整

celling是向上取整,对于正数就是将其小数点去掉,整数部分+1,对于负数就是直接将小数点去掉

floor是向下取整,对于正数就是将其小数点去掉,对于负数就是直接将小数点去掉,整数部分+1,

四、其他函数

user()查看当前用户

database();

查看当前所处的数据库

md5密码加密

md5函数用于对一个字符串进行md5摘要,摘要后得到一个32位字符串,这样能够防止当数据库被盗后,能够初步防止数据密码被盗取

sql 复制代码
mysql> create table user(
    -> id int unsigned primary key auto_increment,
    -> password varchar(32)
    -> );

接着向其中插入没通过md5转化和通过md5转化的字符串看看:

sql 复制代码
insert into user (password) values md5('hehe');
insert into user (password) values (md5('hello'));

这里需要注意的是,当插入的SQL语句中,有password的时候,是无法再历史消息中找到的

接着查看user表

如果想查询的话,就可以通过密码查询,如果事先已经通过md5转化了就需要通过md5查询,没有则不需要

ifnull函数,如果第一个是空,就返回第二个,如果第一个参数不是空,就返回第一个

相关推荐
数据小吏3 小时前
第十五章:数据治理之数据目录:摸清家底,建立三大数据目录
大数据·数据库·人工智能
苹果酱05674 小时前
Java设计模式:探索编程背后的哲学
java·vue.js·spring boot·mysql·课程设计
五花肉村长5 小时前
Linux-读者写著问题和读写锁
linux·运维·服务器·开发语言·数据库·visualstudio
五步晦暝6 小时前
【Excel 支持正则的方法】解决VBA引入正则的方法和步骤
数据库·mysql·excel
卡戎-caryon6 小时前
【MySQL】07.表内容的操作
linux·网络·数据库·mysql·存储引擎
一只fish7 小时前
MySQL 8.0 OCP 1Z0-908 题目解析(11)
数据库·mysql
麓殇⊙7 小时前
黑马点评--基于Redis实现共享session登录
数据库·redis·firefox
zhutoutoutousan7 小时前
解决 Supabase “permission denied for table XXX“ 错误
javascript·数据库·oracle·个人开发
泽韦德7 小时前
【MySQL】第8节|Innodb底层原理与Mysql日志机制深入剖析(一)
数据库·mysql
vvilkim7 小时前
MongoDB 数据库迁移:完整指南与最佳实践
数据库·mongodb