MySQL数据库——内置函数

1.日期函数

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

sql 复制代码
-- 年月日
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2024-11-08     |
+----------------+
1 row in set (0.00 sec)

-- 时分秒
mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 17:54:51       |
+----------------+
1 row in set (0.00 sec)

-- 时间戳
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2024-11-08 17:55:00 |
+---------------------+
1 row in set (0.00 sec)

-- 日期加日期
mysql> select date_add('2024-11-8',interval 10 day);
+---------------------------------------+
| date_add('2024-11-8',interval 10 day) |
+---------------------------------------+
| 2024-11-18                            |
+---------------------------------------+
1 row in set (0.00 sec)

-- 日期减日期
mysql> select date_sub('2024-11-8',interval 10 day);
+---------------------------------------+
| date_sub('2024-11-8',interval 10 day) |
+---------------------------------------+
| 2024-10-29                            |
+---------------------------------------+
1 row in set (0.00 sec)

-- 相差
mysql> select datediff('2024-11-8','2023-11-18');
+------------------------------------+
| datediff('2024-11-8','2023-11-18') |
+------------------------------------+
|                                356 |
+------------------------------------+
1 row in set (0.00 sec)

2.字符串函数

|------------------------------------------|--------------------------------|
| 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的postion开始,取length个字符 |
| ltrim(string) rtrim(string) trim(string) | 去除前空格或后空格 |

3.数学函数

|--------------------------------|----------------------|
| 函数名称 | 描述 |
| 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) | 取模,求余 |

sql 复制代码
-- 绝对值
mysql> select abs(100.2);
+------------+
| abs(100.2) |
+------------+
|      100.2 |
+------------+
1 row in set (0.00 sec)

-- 向上取整
mysql> select ceiling(23.04);
+----------------+
| ceiling(23.04) |
+----------------+
|             24 |
+----------------+
1 row in set (0.00 sec)

-- 向下取整
mysql> select floor(23.7);
+-------------+
| floor(23.7) |
+-------------+
|          23 |
+-------------+
1 row in set (0.00 sec)

-- 保留两位小数位数,小数四舍五入
mysql> select format(12.3456,2);
+-------------------+
| format(12.3456,2) |
+-------------------+
| 12.35             |
+-------------------+
1 row in set (0.00 sec)

-- 产生随机数
mysql> select rand();
+---------------------+
| rand()              |
+---------------------+
| 0.41486722269548776 |
+---------------------+
1 row in set (0.00 sec)

4.其他函数

  • user():查询当前用户
  • md5(str):对一个字符串进行md5摘要,摘要后得到一个32位字符串
  • database():显示当前正在使用的数据库
  • password():函数,MySQL数据库使用该函数对用户加密
  • ifnull(val1,val2):如果val1为null,返回val2,否则返回val1的值
相关推荐
琳少_1 小时前
java程序开发笔记
java·开发语言·数据库·笔记
ZhengEnCi2 小时前
覆盖索引怎么设计:从回表代价到 MySQL 与 PostgreSQL 的落地写法
sql·mysql·postgresql
FungLeo3 小时前
FengCMS · PHP+MySQL升级实录 ·并发一致性连环坑:模板缓存、排队保存、备份恢复的竞态
mysql·缓存·php·工程实践·fengcms
Pocker_Spades_A4 小时前
工业物联网时序数据库选型:一份能直接抄走的技术规格书
数据库·物联网·时序数据库
苜蓿muxu5 小时前
缓存三大问题:穿透、击穿、雪崩
数据库·redis
FungLeo5 小时前
FengCMS · PHP+MySQL 升级实录·模型导入从“加密 SQL 码“到.fengmodel.json
sql·mysql·php·工程实践·fengcms
正在走向自律6 小时前
MySQL兼容性深度实践:建表、增删改查与迁移避坑全记录
数据库·sql·mysql·信创·数据库迁移
用户79384281066086 小时前
城市租房 EDA 实战|从脏数据清洗到 MySQL 入库
mysql
隔窗听雨眠6 小时前
openGauss开源五年的内核演进与生态工程
数据库·opengauss·国产数据库
Decay__7 小时前
PostgreSQL 体系架构全解 —— 进程模型、共享内存、MVCC 与 WAL 预写日志深入
运维·数据库·sql·mysql·架构