博客
关于我
MySQL常见函数
阅读量:796 次
发布时间:2023-02-12

本文共 2568 字,大约阅读时间需要 8 分钟。

MySQL常用函数总结

1. 字符函数

1.1 length(str)

获取字符串的字节数

select length('abcd'); -- 返回值:4select length('数据库abc'); -- 返回值:与字符编码类型有关

1.2 concat(str1, str2, ...)

连接多个字符串

select concat('abc', 'def'); -- 返回值: 'abcdef'

1.3 upper(str) 和 lower(str)

将字符串转换为大写或小写

select upper('HelloWorld'); -- 返回值: HELLOWORLDselect lower('HELLOWORLD'); -- 返回值: helloworld

1.4 substr 和 substring

字符串截取

-- 从第4个字符开始到最后select substr('目标字符串',4); -- 注意:MySQL索引从1开始-- 从第二个字符开始截取1个字符select substr('目标字符串',2,1);

1.5 instr(str, substring)

查找子字符串在源字符串中的起始位置

select instr('目标字符串','子字符串'); -- 返回结果为子字符串起始位置,找不到则返回0

1.6 trim(str)

去掉字符串前后空格

select trim(' aaaa '); -- 返回值: 'aaaa'

1.7 replace(str, oldstr, newstr)

替换字符串中的子串

select replace('目标字符串','字符','zifu');

2. 数学函数

2.1 round()

四舍五入

select round(1.45); -- 返回值: 1select round(1.567,2); -- 返回值:1.57

2.2 ceil() 和 floor()

向上取整和向下取整

select ceil(-1.3); -- 返回值: -1select floor(1.65); -- 返回值:1

2.3 truncate()

截断小数点

select truncate(1.65,1); -- 返回值:1.6

3. 日期函数

3.1 获取系统时间

select now(); -- 返回当前日期时间select curdate(); -- 返回当前系统日期select curtime(); -- 返回当前系统时间select unix_timestamp(now()); -- 返回日期时间转时间戳

3.2 单独获取年/月/日

select YEAR(NOW()); -- 返回年份select month(now()); -- 返回月份(1到12)select day(now()); -- 返回日期(几号)select hour(now()); -- 返回小时select minute(now()); -- 返回分钟select second(now()); -- 返回秒数select monthname(now()); -- 返回月份名称select weekday(now()); -- 返回星期几(0为星期一)select weekofyear(now()); -- 返回本年的第几个星期

3.3 字符转日期

select str_to_date('2018-1-12','%y-%m-%d');

3.4 日期格式化

select date_format(now(),'%Y-%m-%d %H:%i:%s');

4. 流程控制函数

4.1 IF函数

select if('10>1','大','小');

4.2 CASE函数

--案例:员工表中,部门号是30,显示工资为1.1倍;部门号是40,显示工资为1.2倍;其他显示原工资select salary as 原工资, department_id, case department_idwhen 30 then salary*1.1when 50 then salary*1.2else salaryend as 新工资from employees;

4.3 多重CASE

select salary, casewhen salary>20000 then 'A级工资'when salary>10000 then 'B级工资'else 'C级工资'end as 工资等级from employees;

5. IFNULL()函数用法

select ifnull(expr1, expr2); -- 如果expr1不为NULL,返回expr1,否则返回expr2

6. 其他函数

6.1 修改主键初始值

alter table t_student auto_increment = 114;

6.2 中文排序

select distinct name from t_student order by convert(name using gbk);

6.3 随机获取数据

select * from (select * from t_student order by rand()) agroup by a.name;

查询时间范围

-- 获取今天的数据select * from 表名 where to_days(create_time) = to_days(now());-- 获取本周的数据select * from 表名 where yearweek(date_format(submittime, '%Y-%m-%d')) = yearweek(now());-- 获取本月的数据select * from 表名 where date_format(pudate, '%Y%m') = date_format(curdate(), '%Y%m');

最新案例:获取用户当天的总积分

select sum(integral) from t_userwhere enabled = 1and to_days(create_time) = to_days(now())and id = 1;

转载地址:http://audfk.baihongyu.com/

你可能感兴趣的文章
MySQL引擎讲解
查看>>
Mysql当前列的值等于上一行的值累加前一列的值
查看>>
MySQL当查询的时候有多个结果,但需要返回一条的情况用GROUP_CONCAT拼接
查看>>
MySQL必知必会(组合Where子句,Not和In操作符)
查看>>
MySQL必知必会总结笔记
查看>>
MySQL快速入门
查看>>
MySQL快速入门——库的操作
查看>>
mysql快速复制一张表的内容,并添加新内容到另一张表中
查看>>
mysql快速查询表的结构和注释,字段等信息
查看>>
mysql怎么删除临时表里的数据_MySQL中关于临时表的一些基本使用方法
查看>>
mysql性能优化
查看>>
mysql性能优化学习笔记-存储引擎
查看>>
MySQL性能优化必备25条
查看>>
Mysql性能优化(1):SQL的执行过程
查看>>
Mysql性能优化(2):数据库索引
查看>>
Mysql性能优化(3):分析执行计划
查看>>
Mysql性能优化(4):优化的注意事项
查看>>
Mysql性能优化(5):主从同步原理与实现
查看>>
Mysql性能优化(6):读写分离
查看>>
MySQL性能优化(八)--
查看>>