浅谈MySQL函数

目录

 主要mysql函数介绍又以下:

  • 数学函数
  • 字符串函数
  • 时间函数
  • 加密函数

1、数学函数

注意: 每个函数前面都需要加 : select

数学函数

  • abs() 返回绝对值 如: (-100) 值 : 100
  • pi() 返回π的圆规率 如 (不用写) 值 : 3.1415926
  • ceil() 向上取整数 如:(3.14) 值 :4 ( 注意第三个是i )
  • floor() 向下取整数 如: (3.14) 值 :3
  • pow(x,y) x的y次方 如(2,3) 值 :8
  • rand() 随机返回0-1值 如 :() 值 : 0.018137501569592863
  • truncate(x,y) x保留y位小数 如 :(3.1415926,3) 值 :3.141
-- abs() 绝对值
select abs (-100);

-- pi() 返回圆 π 规率
select pi();

--  sqrt ()返回非负数x的二次方
select sqrt(2);

--  pow 返回 x的 y 次乘方 需要
select pow(2,10);
select power(2,10)

-- cell() or 
select ceil(3.14);  -- 向上取整数

-- floor()
select floor(3.14); -- 向下取整数

-- round() 四舍五入取整数 还可以保留小数
select round(3.4);
select round(3.5);
select round(3.7,2)


-- pow() x 的 y 次方
select pow(2,3);


-- random 随机0到1 
select rand();

-- truncate() 保留小数 选择 
select truncate(3.14159265758,3);
select truncate(rand()* 1000 ,3);

2、字符串函数

字符串函数

  • length() 获取长度 如:(’abc’) 值为: 3
  • char_length 长度 如:(’add’) 值为: 3
  • upper() 字符串里英文全大写 (‘abcde’) 值:abcde
  • lower() 字符串里英文全小写 ()
  • trim() 去空格
  • reveres() 反转
  • replace(str,len1,len2) len1代替len2
  •  substrlng(y,z) 截取y 到 z
-- 字符串函数  -- right

select length('fdsajfadslksdafk');

select char_length('fdsajfadslksdafk');

-- lower upper  --全部变成大写、小写
select upper('fdsajfadslksdafk');
select lower('afasdf');

-- trim()  去两端空格
select trim( ' abc ' )
select trim('    afdjkadsfjkll  asd;fasf     ');

-- reverse() 反转
select reverse('abcdefg');
select reverse(trim('    afdjkadsfjkll  asd;fasf     '));、


-- replace(str,len1,len2) len1 代替 len2
select replace('qq群个个都是人才,说话又好听' , '人才', '鬼才');

select replace('路演其是班草', '阿呆呆', '曹某某');

--  截取 y 截取 z 
select substring('监x里个个都是人才,说话又好听',4,5);

3、日期函数

日期函数

  • sysdate() 系统日期+时间
  • curdate() 系统日期
  • curetime() 系统时间
  • weekday() 返回日期参数 注意时间:0-6需要+1
  • dayname() 系统告诉你 今天星期几
  • year() 查年
  • month() 查月
  • day()查日
  • hour()查时
  • minute() 查分
  • secound() 查秒
  • week() 查周

adddate(date,interval,值) 添加到后面得日期 如 : (now(),interval,50 year)

​ datediff(date,date) 连个值得相隔 如 : abs(datediff(‘2005-8-26′,’2021-9-23’))

-- 日期函数

-- 系统当前日期 sysdate()
select sysdate() 系统当前日期; -- 要 时分秒 
-- (日期和时间)
select curdate() 系统当前日期; -- 当前的日期 
-- (日期)
select curtime() 系统当前时间 ;
-- (时间)

--  dayofweek() 今天时本周week -月month - 年year
select dayofyear(sysdate()) 今天是这一年的第几天;
select dayofmonth(sysdate()) 今天是这一月的第几天; 
select dayofweek('2021-9-19')  今天是这一周的第几天; -- 1-7

-- weekday 
select weekday(sysdate()) + 1 今天是星期几; -- 0-6

-- dayname 星期几 
select dayname('2001-9-18') 

-- 年月日时分秒 year month day hour minute second
select year(sysdate()) 年;
select month(sysdate()) 月;
select day(sysdate()) 日;

select hour(sysdate()) 时;
select minute(sysdate()) 分;
select second(sysdate()) 秒; 
-- 周 
select week(sysdate()) 周; 


-- adddate(date,interval expr type)  添加时间后的日期
-- 假设你还有50年寿命,50年后这个时间是多少
select adddate(now(),interval 50 year);
select adddate(now(), interval 50 second);

-- datediff(date1, date2) 两个时间的间隔  
select abs(datediff('2005-8-26','2021-9-23')); -- 1984 - 3 - 24


-- 假设路演其出生时间2005年7月15 请问他到选择活了多少分钟
-- 假设他还有80年的寿命 80年后是多久

select  abs(datediff('2005-7-15',sysdate()) ) * 1444  ;

select  adddate(now(), interval 80 year);

4、加密函数

  • password(str) 加密后不可以逆转
  • md5(str) 加密后可以逆转 注:以前不许
  • charset() 查看mysel 版本
-- 加密函数

select password('123456') 加密后不可以逆转;
select password('666666') 加密后不可以逆转;

-- md5
select password('123456') 加密后不可以逆转 md5('123456') 加密后可以逆转

-- 版本查看
select version();
select charset('123456');


-- md5 加密
-- 雪花推特算法 

到此这篇关于浅谈mysql函数的文章就介绍到这了,更多相关mysql函数内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐