MySQL:内置函数

目录

日期函数

current_date

current_time

current_timestamp

date_add

date_sub

datediff

now

字符串函数

charset

concat

instr

[ucase / lcase](#ucase / lcase)

left

length

replace

strcmp

substring

[ltrim / rtrim / trim](#ltrim / rtrim / trim)

数学函数

abs

bin

hex

conv

ceiling

floor

format

rand

mod

其它函数

user

md5

database

password

ifnull


日期函数

current_date

只返回日期,不含时间,括号可省。

cpp 复制代码
select current_date;     -- 2025-07-21

current_time

只返回时间,不含日期。可以加 0-6 表示秒的小数位。

cpp 复制代码
select current_time;       -- 14:36:17
select current_time(3);    -- 14:36:17.123

current_timestamp

返回完整的日期时间,同样支持精度参数。

cpp 复制代码
select current_timestamp;   -- 2025-07-21 14:36:17
select current_timestamp(2);-- 2025-07-21 14:36:17.12

date_add

把给定日期往后加一段时间。

cpp 复制代码
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

把给定日期往前减一段时间。

cpp 复制代码
select date_sub('2025-07-21', interval 5 day);  -- 2025-07-16

datediff

计算两个日期之间差多少天,结果是整数,正负号表示先后。

cpp 复制代码
select datediff('2025-07-26', '2025-07-21');  -- 5
select datediff('2025-07-18', '2025-07-21');  -- -3

now

返回当前日期和时间

cpp 复制代码
select now();        -- 2025-07-21 15:42:11
select now(3);       -- 2025-07-21 15:42:11.123

字符串函数

charset

查出某个字符串或列实际用的字符集。

cpp 复制代码
select charset('abc');            -- utf8mb4
select charset(name) from user limit 1;

concat

把多个字符串无缝拼在一起。

cpp 复制代码
select concat('hello', ' ', 'world');   -- hello world
select concat(first_name, last_name) as full_name from user;

instr

返回子串第一次出现的位置(从 1 开始计数)。

cpp 复制代码
select instr('banana', 'na');      -- 3
select instr('banana', 'xyz');     -- 0

ucase / lcase

整串转大写 / 转小写。

cpp 复制代码
select ucase('hello');   -- HELLO
select lcase('MySQL');   -- mysql

left

从左起截 n 个字符。

cpp 复制代码
select left('abcdef', 3);   -- abc

length

返回字节长度。

cpp 复制代码
select length('abc');      -- 3
select length('你好');     -- 6  (utf8mb3)

utf8汉字占3个字节

replace

把目标串里所有匹配的子串替换成新的。

cpp 复制代码
select replace('hello world', 'world', 'mysql');   -- hello mysql

strcmp

按字典序比较两个字符串,返回 0(相等)、-1(第一个小)、1(第一个大)。

cpp 复制代码
select strcmp('a', 'b');     -- -1
select strcmp('xyz', 'xyz'); -- 0

substring

截子串,支持从任意位置开始、任意长度。

cpp 复制代码
select substring('abcdef', 2, 3);  -- bcd
select substring('abcdef', -2);    -- ef  (负数从右数)

ltrim / rtrim / trim

清掉左边空格 / 清掉右边空格 / 清掉两端空格。

cpp 复制代码
select ltrim('  abc  ');        -- 'abc  '
select rtrim('  abc  ');        -- '  abc'
select trim('  abc  ');         -- 'abc'
select trim(leading 'x' from 'xxxa'); -- 'a'

数学函数

abs

取绝对值,正负号直接扔掉。

cpp 复制代码
select abs(-7.3);   -- 7.3

bin

把十进制整数变成二进制字符串。

cpp 复制代码
select bin(10);     -- 1010

hex

数字版:把十进制整数变成十六进制字符串。

cpp 复制代码
select hex(255);        -- 'ff'

字符串版:把字符串的每个字节变成两位的十六进制。

cpp 复制代码
select hex('abc');      -- 616263

conv

任意进制互转。

cpp 复制代码
select conv('a', 16, 2);   -- '1010'  (16→2)
select conv(10, 10, 16);   -- 'a'     (10→16)

ceiling

向上取整,带小数就进一位。

cpp 复制代码
select ceiling(3.14);   -- 4
select ceiling(-2.3);   -- -2

floor

向下取整,直接抹掉小数。

cpp 复制代码
select floor(3.99);   -- 3
select floor(-2.3);   -- -3

format

把数字按千分位+四舍五入后变字符串,常用于报表。

format(x, 小数位[, 区域])

cpp 复制代码
select format(1234.5678, 2);   -- '1,234.57'

rand

随机浮点数,范围 0 ≤ x < 1。

cpp 复制代码
select rand();        -- 0.923473... 每次不同
select rand(5);       -- 给定种子后结果可复现

mod

取余数;功能同 % 运算符。

cpp 复制代码
select mod(10, 3);   -- 1
select mod(-10, 3);  -- -1

其它函数

user

返回当前登录 mysql 的「用户@主机」字符串。

cpp 复制代码
select user();      -- 'root@localhost'

md5

把任意字符串算成 32 位 16 进制 md5 摘要,常用于一次性密码或校验。

cpp 复制代码
select md5('123456');    -- 'e10adc3949ba59abbe56e057f20f883e'

database

告诉你此刻默认库的名字。

cpp 复制代码
use test;
select database();     -- 'test'

password

生成 41 位加密哈希。

cpp 复制代码
select password('abc');  -- '*0d3d7e8a7b3d...'

ifnull

遇到 null 就替换成指定值 。

cpp 复制代码
select ifnull(null, 0);      -- 0
select ifnull(score, 0) from student;

相关推荐
这周也會开心1 小时前
SQL-窗口函数
数据库·sql
TDengine (老段)3 小时前
TDengine 时间函数 WEEKDAY() 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
TDengine (老段)3 小时前
从 ETL 到 Agentic AI:工业数据管理变革与 TDengine IDMP 的治理之道
数据库·数据仓库·人工智能·物联网·时序数据库·etl·tdengine
LQ深蹲不写BUG5 小时前
MySql的事务机制
数据库·mysql
逼子格6 小时前
【Proteus仿真】定时器控制系列仿真——秒表计数/数码管显示时间
数据库·单片机·嵌入式硬件·51单片机·proteus·定时器·硬件工程师
stein_java7 小时前
Mybatis-7 XML映射器
数据库·sql·mybatis
xhbh6667 小时前
开发效率翻倍:资深DBA都在用的MySQL客户端利器
数据库·mysql·数据库连接工具·mysql 连接工具
LJC_Superman8 小时前
Web与Nginx网站服务
运维·服务器·前端·网络·数据库·nginx·vim
java水泥工8 小时前
校园管理系统|基于SpringBoot和Vue的校园管理系统(源码+数据库+文档)
数据库·vue.js·spring boot