MySql常用函数
一、数学函数
1 ABS(X):返回X的绝对值,大小写均可。
select abs(-2)
2 MOD(N,M)或%:返回N被M除的余数。
- %
select 3 % 2
- mod(m,n)
select mod(7,5)
3 CEILING(X):返回不小于X的最小整数值。
select ceiling(1.23);
select ceiling(-1.23);
4 ROUND(X) :返回参数X四舍五入后的结果。
select round(1.58);
二、字符串函数
1 ASCII(str):返回字符串str的最左面字符的ASCII代码值。如果str是空字符串,返回0。如果str是NULL,返回NULL。
-- 返回首数字2的ascii
select ascii(22);
-- 返回首字符a的ascii
select ascii('abc');
2 CONCAT(str1,str2,…):字符串拼接,如果有一个参数为null,则拼接结果都为null。
select concat('My', 'S', 'QL');
select concat('My', null);
3 LENGTH(str):返回字符串str的长度。
select length('abcd')
4 LOCATE(substr, str):返回子串substr在字符串str第一个出现的位置,如果substr不在str里面,返回0.
str:字符串。
substr:字符串子串。
select locate('ql','mysql')
5 INSTR(str, substr):返回子串substr在字符串str中的第一个出现的位置。
select instr('foobarbar', 'bar');
6 LEFT(str,len):返回字符串str的最左面len个字符。同样right(str, len)返回右边len个字符。
select LEFT(‘foobarbar’, 5);
7 SUBSTRING(str,pos):以pos为起始位置,返回str后面的字符串。
select substring('springboot',3)
8 TRIM(str):返回字符串str,原有字符串前面和后面的空格被去除。
如果为LTRIM(str):返回str删除了前面空格的字符串。
如果为RTRIM(str):返回str删除了后面空格的字符串。
select trim(' b a d ')
9 REPLACE(str, str1, str2):将str字符串中的所有str1字符替换为str2,然后返回。
select replace('www.mysql.com', 'w', 'W');
10 REPEAT(str,count):str字符串重复count次返回,如果str或count是NULL,返回NULL。
select repeat('mysql',3)
11 REVERSE(str):字符反转。
select reverse('mysql')
12 INSERT(str,index,len,newstr):原有字符串str,从第index个位置开始,把接下来长度为len的字符串替换成newstr。
select insert('springcloud',7,5,'boot')
三、日期和函数
1 DAYOFWEEK(date):返回日期date的星期索引(1=星期天,2=星期一, …7=星期六)。
select dayofweek(now());
select daofweek('2023-02-13')
2 WEEKDAY(date):返回date的星期索引(0=星期一,1=星期二, ……6= 星期天)。
select weekday(now())
select weekday('2023-02-13')
3 DAYOFMONTH(date):返回date月份中的日期,在1到31范围内。
select dayofmonth('2023-02-13')
4 MONTH(date):返回date的月份,范围1到12。
select month('2023-02-13')
5 YEAR(date):返回date的年份,范围在1000到9999。
select year('2023-02-12')
6 DAYOFYEAR(date):返回date日期具体是在一年中的哪一天, 在1到366范围内。
select dayofyear('2023-02-13')
7 QUARTER(date):返回date在一年中的季度,范围1到4。1代表第一季度,2代表第二季度…
select quarter('2023-02-13');
8 HOUR(time):返回time的小时,范围是0到23。
select hour(time) --返回时间的小时
select minute(time) --返回时间的分钟
select second(time) --返回时间的秒数
9 日期加减。
- DATE_ADD(date,INTERVAL expr type) ,进行日期增加的操作,可以精确到秒。
-- 加一个小时
select date_add('2023-02-13', interval 1 hour) --2023-02-13 01:00:00
-- 加一天
select date_add('2023-02-13', interval 1 day) --2023-02-14
-- 加一个月
select date_add('2023-02-13', interval 1 month) --2023-03-13
-- 加一年
select date_add('2023-02-13', interval 1 year) --2024-02-14
- DATE_SUB(date,INTERVAL expr type) ,进行日期减少的操作,可以精确到秒。
-- 减一个小时
select date_sub('2023-02-13', interval 1 hour) --2023-02-12 23:00:00
-- 减一天
select date_sub('2023-02-13', interval 1 day) --2023-02-12
-- 减一个月
select date_sub('2023-02-13', interval 1 month) --2023-01-13
-- 减一年
select date_sub('2023-02-13', interval 1 year) --2022-02-13
10 CURRENT():以‘YYYY-MM-DD’或YYYYMMDD格式返回今天日期值,取决于函数在一个字符串还是数字上下文被使用。
--无参数
select curdate() --2023-02-13
11 CURTIME();以‘HH:MM:SS’或HHMMSS格式返回当前时间值。
--无参数
select curtime(); --18:46:27
12 NOW():以’YYYY-MM-DD HH:MM:SS’或YYYYMMDDHHMMSS格式返回当前的日期和时间。
YYYY-MM-DD HH:MM:SS(年-月-日 小时:分钟:秒数)
--无参数
select now() --2023-02-13 18:50:54
五、系统信息函数
1 select version():获取数据库的版本号;
2 select connection_id():服务器的连接数,也就是到现在为止MySQL服务的连接次数;
3 select user(),
select system_user():获取用户名;
六、加密函数
1 select password(‘字符串’):加密函数,mysql8版本已弃用;
2 select md5(‘字符串’):md5加密;