MySQL学习笔记_7_MySQL常用内置函数

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: MySQL常用内置函数 说明: 1)可以用在SELECT/UPDATE/DELETE中,及where,orderby,having中 2)在函数里将字段名作为参数,变量的值就是字段所对应的每一行的值。

MySQL常用内置函数



说明:

1)可以用在SELECT/UPDATE/DELETE中,及whereorderbyhaving

2)在函数里将字段名作为参数,变量的值就是字段所对应的每一行的值。

3)在程序设计语言如C++中提供的函数,MySQL大部分也提供了,关于MySQL函数的完整信息,请参阅《MySQL参考手册》



一、字符串函数【比较常用,需要掌握】

1 concat(s1,s2,...,sn) #把传入的参数连接成一个字符串

selectconcat('abc','def');

selectconcat(name,' age is ',age) from users;



2insert(str,m,n,inser_str) #str的从m位置开始的n个字符替换为inser_str

selectinsert('abcdef',2,3,'123456');

selectinsert(name,3,2,'HAHA') from users;

selectinsert(name,2,2,'00') from users;



3lower(str)/upper(str) #将字符串str转换成小写/大写

selectlower('HELLO'),upper('hello');

selectlower('HELLO') as 'HELLO'upper('hello')as 'HELLO';

select* from users where upper(name) = 'AAA';



4left(str,n)/right(str,n) #分别返回str最左边/最右边的n个字符,如果n<=> NULL 则任何东西不返回

selectleft('123',3),right('123456',3),left('123',NULL);



5lpad(str,n,pad)/rpad(str,n,pad) #用字符串padstr的最左边/最右边进行填充,知道满足str含有n个字符为止

selectname,lpad(name,10,'#'),rpad(name,10,'@') from users;



6trim(str)/ltrim(str)/rtrim(str) #去除字符串str左右空格/左空格/右空格

selectconcat('#',trim(" abc "),'#'),concat('#',ltrim(' abc '),'#'),concat('#',rtrim(' abc '),'#');



7replace(str,sear_str,sub_str) #将字符串str中所有出现的sear_str字符串替换为sub_str

select replace('abcdefgabcd','cd','XXX') ;



8strcmp(str1,str2) #ASCII码比较字符串str1str2,返回-1str1< str2/0(str1= str2)/1(str1 > str2)

selectstrcmp('aa','bb'),strcmp('aa','aa'),strcmp('bb','aa');



9substring(str,n,m) #返回字符串str中从n起,m个字符长度的字符串

selectsubstring('abcdef',2,3);

selectname,substring(name,1,2) as subname from users;



二、数值函数

1abs(x) #返回x的绝对值

selectabs(10),abs(-10);

selectabs(age) from users;



2ceil(x) #返回大于x的最小整数

3floor(x) #返回小于x的最大整数

selectceil(2.1),ceil(2.5),ceil(2.9),floor(2.1),floor(2.5),floor(2.9);



4mod(x,y) #返回x/y的模,与x%y作用相同

selectmod(null,11);



5rand() #返回01之间的随机数

selectrand();

selectceil(rand() * 100); #0100之间的整数随机数

selectfloor(rand() * 100);



6round(n,m) #返回n四舍五入之后含有m位小数的值,m值默认为0

selectround(1.23);

selectround(1.456,2);



7truncate(n,m) #返回数字n被截断为m位小数的数值

selecttruncate(1.234,2);

selecttruncate(1.235,2),round(1.235,2);



三、日期函数

1curdate() #返回当前日期

2curtime() #返回当前时间

selectcurdate(),curtime();



3now() #返回当前日期+时间

selectnow();



4unix_timestamp(now())#返回unix当前时间的时间戳

selectunix_timestamp(now()); #从计算机元年(1971-1-1000000)到现在的秒数



5from_unixtime() #将时间戳(整数)转换为“日期+时间(xx-xx-xxxx:xx:xx)”的形式

selectfrom_unixtime(1392853616);



6week(now()) #返回当前时间是第几周

7year(now()) #返回当前是XX

8hour(now())/hour(curtime()) #返回当前时间的小时数

9minute(curtime()) #返回当期的分钟数

...

selectweek(now()),year(now()),hour(now());

selectweek(from_unixtime(1392853616)); #返回unix时间戳中的周期数



10monthname(now())/monthname(curdate()) #返回当前月的英文名



11date_format(now(),"%Y-%M-%D%H:%I%S") #将当期时间格式化

selectdate_format(now(),"%Y-%m-%d %H:%i%s");

selectdate_format(now(),"%y%m%d %H:%i%s");

四、流程控制函数

1if(value,true,false) #如果value值为真,则返回true,否则,返回false

selectif (salary > 3000,'Hight','Low') from salary;

selectid,salary, if (salary <=> NULL,'NULL','NOT NULL') from salary;



2ifnull(value1,value2)#如果value1不为空,则返回value1,不然返回value2

#可以用来进行空值替换

selectifnull(salary,0.00) from salary;



3casewhen [value] then … else …end #如果value值为真,执行then之后的语句,不然执行eles后的语句,不要忘记end

selectcase when salary <= 3000 then "Low" else "Hight"end from salary;



五、其他函数

1database() #当前数据库

2version() #当前数据库版本

3user() #当前登录用户

selectdatabase();



4inet_aton(ip) #ip地址的网络字节顺序

selectinet_aton('192.168.139.1');



5inet_ntoa #返回数字所代表的ip

selectinet_ntoa(3232271105);



6password(str) #返回加密的str字符串

selectpassword("123456"); #返回一个41位长的加密字符串,只是用于给MySQL系统用户进行加密

7md5() #在应用程序中进行数据加密,比如在C++程序中

selectmd5(“123456”);

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
关系型数据库 MySQL 索引
936. 【mysql】locate函数
936. 【mysql】locate函数
18 2
|
1月前
|
SQL 关系型数据库 MySQL
927. 【mysql】coalesce 函数
927. 【mysql】coalesce 函数
21 3
|
1月前
|
关系型数据库 MySQL
926.【mysql】 date 函数
926.【mysql】 date 函数
59 3
|
1月前
|
关系型数据库 MySQL
925. 【mysql】convert 函数
925. 【mysql】convert 函数
23 3
|
2月前
|
关系型数据库 MySQL 索引
mysql常见函数
mysql常见函数
|
1月前
|
SQL 关系型数据库 MySQL
【MySQL 数据库】2、MySQL 的数据控制语言、函数和约束
【MySQL 数据库】2、MySQL 的数据控制语言、函数和约束
28 0
|
2月前
|
存储 SQL 关系型数据库
MySQL技能完整学习列表7、存储过程和函数——1、存储过程(Stored Procedures)的创建和执行——2、函数(Functions)的创建和使用
MySQL技能完整学习列表7、存储过程和函数——1、存储过程(Stored Procedures)的创建和执行——2、函数(Functions)的创建和使用
34 0
|
3天前
|
SQL 关系型数据库 MySQL
DQL语言之常见函数(mysql)
DQL语言之常见函数(mysql)
|
10天前
|
SQL 关系型数据库 MySQL
mysql多表查询、函数查询
mysql多表查询、函数查询
|
1月前
|
关系型数据库 MySQL 数据库
【mysql】—— 函数的基本介绍
【mysql】—— 函数的基本介绍