MySQL:内置函数
目录
日期函数
current_date
current_time
current_timestamp
date_add
date_sub
datediff
now
字符串函数
charset
concat
instr
ucase / lcase
left
length
replace
strcmp
substring
ltrim / rtrim / trim
数学函数
abs
bin
hex
conv
ceiling
floor
format
rand
mod
其它函数
user
md5
database
password
ifnull
日期函数
current_date
只返回日期,不含时间,括号可省。
select current_date; -- 2025-07-21
current_time
只返回时间,不含日期。可以加 0-6 表示秒的小数位。
select current_time; -- 14:36:17
select current_time(3); -- 14:36:17.123
current_timestamp
返回完整的日期时间,同样支持精度参数。
select current_timestamp; -- 2025-07-21 14:36:17
select current_timestamp(2);-- 2025-07-21 14:36:17.12
date_add
把给定日期往后加一段时间。
select date_add('2025-07-21 12:00:00', interval 3 day); -- 2025-07-24 12:00:00
select date_add('2025-07-21', interval -2 month); -- 2025-05-21
date_sub
把给定日期往前减一段时间。
select date_sub('2025-07-21', interval 5 day); -- 2025-07-16
datediff
计算两个日期之间差多少天,结果是整数,正负号表示先后。
select datediff('2025-07-26', '2025-07-21'); -- 5
select datediff('2025-07-18', '2025-07-21'); -- -3
now
返回当前日期和时间
select now(); -- 2025-07-21 15:42:11
select now(3); -- 2025-07-21 15:42:11.123
字符串函数
charset
查出某个字符串或列实际用的字符集。
select charset('abc'); -- utf8mb4
select charset(name) from user limit 1;
concat
把多个字符串无缝拼在一起。
select concat('hello', ' ', 'world'); -- hello world
select concat(first_name, last_name) as full_name from user;
instr
返回子串第一次出现的位置(从 1 开始计数)。
select instr('banana', 'na'); -- 3
select instr('banana', 'xyz'); -- 0
ucase / lcase
整串转大写 / 转小写。
select ucase('hello'); -- HELLO
select lcase('MySQL'); -- mysql
left
从左起截 n 个字符。
select left('abcdef', 3); -- abc
length
返回字节长度。
select length('abc'); -- 3
select length('你好'); -- 6 (utf8mb3)
utf8汉字占3个字节
replace
把目标串里所有匹配的子串替换成新的。
select replace('hello world', 'world', 'mysql'); -- hello mysql
strcmp
按字典序比较两个字符串,返回 0(相等)、-1(第一个小)、1(第一个大)。
select strcmp('a', 'b'); -- -1
select strcmp('xyz', 'xyz'); -- 0
substring
截子串,支持从任意位置开始、任意长度。
select substring('abcdef', 2, 3); -- bcd
select substring('abcdef', -2); -- ef (负数从右数)
ltrim / rtrim / trim
清掉左边空格 / 清掉右边空格 / 清掉两端空格。
select ltrim(' abc '); -- 'abc '
select rtrim(' abc '); -- ' abc'
select trim(' abc '); -- 'abc'
select trim(leading 'x' from 'xxxa'); -- 'a'
数学函数
abs
取绝对值,正负号直接扔掉。
select abs(-7.3); -- 7.3
bin
把十进制整数变成二进制字符串。
select bin(10); -- 1010
hex
数字版:把十进制整数变成十六进制字符串。
select hex(255); -- 'ff'
字符串版:把字符串的每个字节变成两位的十六进制。
select hex('abc'); -- 616263
conv
任意进制互转。
select conv('a', 16, 2); -- '1010' (16→2)
select conv(10, 10, 16); -- 'a' (10→16)
ceiling
向上取整,带小数就进一位。
select ceiling(3.14); -- 4
select ceiling(-2.3); -- -2
floor
向下取整,直接抹掉小数。
select floor(3.99); -- 3
select floor(-2.3); -- -3
format
把数字按千分位+四舍五入后变字符串,常用于报表。
format(x, 小数位[, 区域])
select format(1234.5678, 2); -- '1,234.57'
rand
随机浮点数,范围 0 ≤ x < 1。
select rand(); -- 0.923473… 每次不同
select rand(5); -- 给定种子后结果可复现
mod
取余数;功能同 % 运算符。
select mod(10, 3); -- 1
select mod(-10, 3); -- -1
其它函数
user
返回当前登录 mysql 的「用户@主机」字符串。
select user(); -- 'root@localhost'
md5
把任意字符串算成 32 位 16 进制 md5 摘要,常用于一次性密码或校验。
select md5('123456'); -- 'e10adc3949ba59abbe56e057f20f883e'
database
告诉你此刻默认库的名字。
use test;
select database(); -- 'test'
password
生成 41 位加密哈希。
select password('abc'); -- '*0d3d7e8a7b3d…'
ifnull
遇到 null 就替换成指定值 。
select ifnull(null, 0); -- 0
select ifnull(score, 0) from student;
完