大家在写 sql 时,经常会遇到处理日期、拼接字符串、数字计算、空值判断等场景。如果手写逻辑又麻烦又容易错,其实 mysql 早就给我们准备好了内置函数,直接调用就能搞定大部分需求。
今天这篇文章,我把 mysql 最常用的日期、字符串、数学、其他常用函数一次性整理好,配案例 + 表格,新手也能直接上手用!
一、日期函数(最常用,必背)
日常开发中,记录时间、计算天数、加减时间全靠它。
| 函数 | 作用 |
|---|---|
| current_date() | 获取当前日期(年月日) |
| current_time() | 获取当前时间(时分秒) |
| current_timestamp() | 当前时间戳(年月日时分秒) |
| now() | 当前日期时间(和 timestamp 效果一致) |
| date(datetime) | 提取日期部分,去掉时间 |
| date_add (date, interval 数值 单位) | 日期加时间 |
| date_sub (date, interval 数值 单位) | 日期减时间 |
| datediff(date1, date2) | 计算两个日期相差天数 |
常用单位
year、month、day、hour、minute、second
快速示例
-- 当前日期
select current_date();
-- 当前时间
select current_time();
-- 当前完整时间
select now();
-- 日期加10天
select date_add('2017-10-28', interval 10 day);
-- 日期减2天
select date_sub('2017-10-1', interval 2 day);
-- 计算日期差
select datediff('2017-10-10', '2016-09-01');
实战小案例
建表存生日
create table tmp(
id int primary key auto_increment,
birthday date
);
insert into tmp(birthday) values(current_date());
留言表:只显示日期、查 2 分钟内发布的帖子
create table msg (
id int primary key auto_increment,
content varchar(30) not null,
sendtime datetime
);
insert into msg(content,sendtime) values('hello1', now());
-- 只显示日期,不显示时间
select content, date(sendtime) from msg;
-- 查询2分钟内发布的帖子
select * from msg where date_add(sendtime, interval 2 minute) > now();
二、字符串函数(拼接、截取、替换)
处理文本、姓名、内容展示时高频使用。
| 函数 | 作用 |
|---|---|
| charset(str) | 返回字符串字符集 |
| concat(s1,s2...) | 字符串拼接 |
| instr(str,sub) | 子串第一次出现位置 |
| ucase/upper | 转大写 |
| lcase/lower | 转小写 |
| left(str,len) | 从左边取 len 个字符 |
| length(str) | 字符串字节长度 |
| replace(str,old,new) | 字符串替换 |
| substring(str,pos,len) | 截取字符串 |
| trim/ltrim/rtrim | 去空格 |
快速示例
-- 拼接字符串
select concat('张三', '的语文是', 90, '分');
-- 字符串长度(字节)
select length('张三');
-- 替换字符
select replace('smith', 's', '上海');
-- 截取第2个字符开始,取2个
select substring('smith', 2, 2);
-- 首字母小写
select concat(lcase(substring(ename,1,1)), substring(ename,2)) from emp;
三、数学函数(计算、取整、随机数)
做统计、报表、数值处理必备。
| 函数 | 作用 |
|---|---|
| abs(x) | 绝对值 |
| ceiling(x) | 向上取整 |
| floor(x) | 向下取整 |
| format(x,n) | 保留 n 位小数(四舍五入) |
| rand() | 0~1 随机小数 |
| mod(x,y) | 取余 / 取模 |
| conv(x,from,to) | 进制转换 |
快速示例
-- 绝对值 select abs(-100.2); -- 向上取整 select ceiling(23.04); -- 向下取整 select floor(23.7); -- 保留2位小数 select format(12.3456, 2); -- 随机数 select rand();
四、其他高频函数(必须会)
这些函数在项目里几乎天天用。
| 函数 | 作用 |
|---|---|
| user() | 当前数据库用户 |
| database() | 当前数据库名 |
| md5(str) | md5 加密(32 位) |
| password() | mysql 用户加密 |
| ifnull(val1,val2) | val1 为 null 则返回 val2,否则返回 val1 |
快速示例
-- 当前用户
select user();
-- 当前库
select database();
-- md5加密
select md5('admin');
-- null处理
select ifnull(null, '默认值');
五、实战 oj 小练习
题目:查找字符串 '10,a,b' 中逗号 ',' 出现的次数。
思路:用原长度 - 去掉逗号后的长度 = 逗号个数。
select
length('10,a,b') - length(replace('10,a,b', ',', '')) as cnt;
六、总结(一张图记住)
- 日期:now / date_add / date_sub / datediff
- 字符串:concat / replace / substring / length
- 数学:abs / ceiling / floor / format / rand
- 通用:ifnull / md5 / user / database
把这些函数背熟,80% 的日常 sql 都能轻松写出来,代码更简洁、效率更高!
到此这篇关于mysql数据库中内置函数的使用全解析的文章就介绍到这了,更多相关mysql内置函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论