MySQL——内置函数

一.日期函数

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(datel, date2) :两个日期的差,单位是天
now():当前日期时间

与日期函数相关的数据类型包括:date(日期)、datetime(日期加时间)、timestamp(时间戳)等


二.字符串函数

chrset(str) :返回字符串字符集
concat(string[,...]) :连接字符串
instr(string,substring) :返回substring在string中出现的位置,没有返回0
ucase(string) :转换成大写
lcase(string) :转换成小写
left(string,length):从string中的左边起取length个字符

right(string,length): 从string中的右边起取length个字符
length(string) :string的长度
replace(str,search_str,replace_str) :在str中用replace_str替换search_str
strcmp(string1,string2) :逐字符比较两字符串大小
substring(str,position,[length]) :从str的postion开始,取length个字符
ltrim(string)、rtrim(string)、trim(string):去除前空格或后空格或前后空格


三.数字函数

abs (number) :绝对值函数
bin(decimal number) :十进制转换二进制
hex(decimalNumber) :转换成十六进制
conv(number,from base,to base) :进制转换
ceiling(number) :向上去整
floor(number) :向下去整
format (number,decimal places) :格式化,保留小数位数
hex(decimalNumber) :转换成十六进制
rand() :返回随机浮点数,范围[0.0,1.0)
mod(number.denominator):取模,求余

使用rand()生成随机数时,可以使其乘以10的倍数来达到生成对应0~10的倍数之内的数字


四.其他函数

user():查询当前用户
md5(str):对一个字符串进行md5摘要,摘要后得到一个32位字符串
database():显示当前正在使用的数据库
password():MySQL数据库使用该函数对用户加密,

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

相关推荐
清酒伴风(面试准备中......)19 分钟前
小白学编程之——数据库如何性能优化
数据库·oracle·性能优化
默心28 分钟前
centos7部署mysql5.7
linux·运维·mysql·centos
The Future is mine1 小时前
SQL Server中delete table和truncate table删除全表数据哪个快?
数据库
瀚高PG实验室1 小时前
HGDB插入超长字段报错指示列名的问题处理
数据库
好吃的肘子1 小时前
MongoDB 高可用复制集架构
数据库·mongodb·架构
兮兮能吃能睡2 小时前
Python之with语句
数据库·python
不穿铠甲的穿山甲2 小时前
MySQL-数据库分布式XA事务
数据库·分布式·mysql
Hadoop_Liang2 小时前
解决Mawell1.29.2启动SQLException: You have an error in your SQL syntax问题
大数据·数据库·maxwell
码上飞扬2 小时前
MongoDB数据库深度解析:架构、特性与应用场景
数据库·mongodb·架构
飞天红猪侠c3 小时前
MySQL-逻辑架构
数据库·mysql