自学MySql内置函数知识点总结

作者:LiuYanYGZ 时间:2024-01-23 11:19:04 

字符串函数

查看字符的ascii码值ascii(str),str是空串时返回0


select ascii('a');

 查看ascii码值对应的字符char(数字)


select char(97);

拼接字符串concat(str1,str2...)


select concat(12,34,'ab');

包含字符个数length(str)


select length('abc');

 截取字符串

  • left(str,len)返回字符串str的左端len个字符

  • right(str,len)返回字符串str的右端len个字符

  • substring(str,pos,len)返回字符串str的位置pos起len个字符


select substring('abc123',2,3);

 去除空格

ltrim(str)返回删除了左空格的字符串str
rtrim(str)返回删除了右空格的字符串str
trim([方向 remstr from str)返回从某侧删除remstr后的字符串str,方向词包括both、leading、trailing,表示两侧、左、右


select trim(' bar ');
select trim(leading 'x' FROM 'xxxbarxxx');
select trim(both 'x' FROM 'xxxbarxxx');
select trim(trailing 'x' FROM 'xxxbarxxx');
SELECT TRIM(LEADING ' ' FROM ' my ');

返回由n个空格字符组成的一个字符串space(n)


select space(10);

替换字符串replace(str,from_str,to_str)


select replace('abc123','123','def');

大小写转换,函数如下

  • lower(str)

  • upper(str)


select lower('aBcD');

数学函数

求绝对值abs(n)


select abs(-32);

求m除以n的余数mod(m,n),同运算符%


select mod(10,3);
select 10%3;

地板floor(n),表示不大于n的最大整数


select floor(2.3);

天花板ceiling(n),表示不小于n的最大整数


select ceiling(2.3);

求四舍五入值round(n,d),n表示原数,d表示小数位置,默认为0


select round(1.6);

求x的y次幂pow(x,y)


select pow(2,3);

获取圆周率PI()


select PI();

随机数rand(),值为0-1.0的浮点数


select rand();

还有其它很多三角函数,使用时可以查询文档

日期时间函数

获取子值,语法如下

  • year(date)返回date的年份(范围在1000到9999)

  • month(date)返回date中的月份数值

  • day(date)返回date中的日期数值

  • hour(time)返回time的小时数(范围是0到23)

  • minute(time)返回time的分钟数(范围是0到59)

  • second(time)返回time的秒数(范围是0到59)


select year('2016-12-21');

日期计算,使用+-运算符,数字后面的关键字为year、month、day、hour、minute、second


select '2016-12-21'+interval 1 day;

日期格式化date_format(date,format),format参数可用的值如下

获取年%Y,返回4位的整数

*获取年%y,返回2位的整数

*获取月%m,值为1-12的整数

获取日%d,返回整数

*获取时%H,值为0-23的整数

*获取时%h,值为1-12的整数

*获取分%i,值为0-59的整数

*获取秒%s,值为0-59的整数


select date_format('2016-12-21','%Y %m %d');

当前日期current_date()


select current_date();

当前时间current_time()


select current_time();

当前日期时间now()


select now();

来源:https://www.cnblogs.com/LiuYanYGZ/p/12237401.html

标签:MySql,内置函数
0
投稿

猜你喜欢

  • ASP.NET(AJAX+JSON)实现对象调用

    2023-07-19 12:29:33
  • python实现随机梯度下降法

    2023-11-02 16:55:37
  • Python函数中的作用域规则详解

    2023-02-14 04:32:28
  • python基础之面对对象基础类和对象的概念

    2021-08-16 03:26:33
  • php预定义常量

    2023-11-14 10:35:27
  • 一文详解Python中的Map,Filter和Reduce函数

    2022-03-02 07:51:28
  • 基于python内置函数与匿名函数详解

    2021-02-02 08:27:26
  • Asp Object 之:AddHeader

    2008-05-05 12:58:00
  • selenium+opencv实现滑块验证码的登陆

    2022-03-28 06:49:04
  • python 面向对象开发及基本特征

    2022-09-03 01:48:43
  • 如果用JS得到字符串中出现次数最多的字母

    2007-12-03 21:01:00
  • SQL 重复记录问题的处理方法小结

    2024-01-16 14:56:36
  • JS FormData对象使用方法实例详解

    2024-02-25 04:54:46
  • 理解 javascript 中的函数表达式与函数声明

    2024-04-23 09:08:26
  • 使用php+swoole对client数据实时更新(一)

    2024-05-03 15:13:08
  • sql server 2000阻塞和死锁问题的查看与解决方法

    2024-01-20 01:46:45
  • Pyscript使用本地Pyodide配置步骤

    2021-06-12 06:58:49
  • pytorch 实现多个Dataloader同时训练

    2023-11-14 23:33:21
  • mysql一次将多条不同sql查询结果并封装到一个结果集的实现方法

    2024-01-19 23:30:05
  • 优化SQL Server的内存占用之执行缓存

    2012-04-13 11:45:06
  • asp之家 网络编程 m.aspxhome.com