当前位置:网站首页>使用函数
使用函数
2022-08-04 11:16:00 【parker_001】
MySQL中提供了大量函数来简化用户对数据库的操作,比如字符串的处理、日期的运算、数值的运算等等。使用函数可以大大提高SELECT语句操作数据库的能力,同时也给数据的转换和处理提供了方便。 (在sql中使用函数)
函数只是对查询结果中的数据进行处理,不会改变数据库中数据表的值。MySQL中的函数主要分为单行函数和多行函数两大类,下面我们将详细讲解这两大类函数。
单行函数
单行函数是指对每一条记录输入值进行计算,并得到相应的计算结果,然后返回给用户,也就是说,每条记录作为一个输入参数,经过函数计算得到每条记录的计算结果。
常用的单行函数主要包括字符串函数、数值函数、日期与时间函数、流程函数以及其他函数。
多行函数
多行函数是指对一组数据进行运算,针对这一组数据(多行记录)只返回一个结果,也称为分组函数。
-- 函数举例:
select empno,ename,lower(ename),upper(ename),sal from emp;
-- 函数的功能:封装了特定的一些功能,我们直接拿过来使用,可以实现对应的功能
-- 函数作用:为了提高select的能力
-- 注意:函数没有改变数据自身的值,而是在真实数据的上面进行加工处理,展示新的结果而已。
select max(sal),min(sal),count(sal),sum(sal),avg(sal) from emp;
-- 函数的分类:
-- lower(ename),upper(ename) :改变每一条结果,每一条数据对应一条结果 -- 单行函数
-- max(sal),min(sal),count(sal),sum(sal),avg(sal):多条数据,最终展示一个结果 -- 多行函数
PS:除了多行函数(max,min,count,sum,avg),都是单行函数
单行函数
1.字符串函数 (String StringBuilder)
2. 数值函数 (Math)
3. 日期与时间函数
-- 单行函数包含:
-- 1.字符串函数
select ename,length(ename),substring(ename,2,3) from emp;
-- substring字符串截取,2:从字符下标为2开始,3:截取长度3 (下标从1开始)
-- 2.数值函数
select abs(-5),ceil(5.3),floor(5.9),round(3.14) from dual; -- dual实际就是一个伪表
select abs(-5) 绝对值,ceil(5.3) 向上取整,floor(5.9) 向下取整,round(3.14) 四舍五入; -- 如果没有where条件的话,from dual可以省略不写
select ceil(sal) from emp;
select 10/3,10%3,mod(10,3) ;
-- 3.日期与时间函数
select * from emp;
select curdate(),curtime() ; -- curdate()年月日 curtime()时分秒
select now(),sysdate(),sleep(3),now(),sysdate() from dual; -- now(),sysdate() 年月日时分秒
insert into emp values (9999,'lili','SALASMAN',7698,now(),1000,null,30);
-- now()可以表示年月日时分秒,但是插入数据的时候还是要参照表的结构的
desc emp;
4. 流程函数( IF SWITCH)
5.JSON函数
6.其他函数
-- 4.流程函数
-- if相关
select empno,ename,sal,if(sal>=2500,'高薪','底薪') as '薪资等级' from emp; -- if-else 双分支结构
select empno,ename,sal,comm,sal+ifnull(comm,0) from emp; -- 如果comm是null,那么取值为0 -- 单分支
select nullif(1,1),nullif(1,2) from dual; -- 如果value1等于value2,则返回null,否则返回value1
-- case相关:
-- case等值判断
select empno,ename,job,
case job
when 'CLERK' then '店员'
when 'SALESMAN' then '销售'
when 'MANAGER' then '经理'
else '其他'
end '岗位',
sal from emp;
-- case区间判断:
select empno,ename,sal,
case
when sal<=1000 then 'A'
when sal<=2000 then 'B'
when sal<=3000 then 'C'
else 'D'
end '工资等级',
deptno from emp;
from emp;
-- 5.JSON函数
-- 6.其他函数
select database(),user(),version() from dual;
边栏推荐
- MySQL不提供数组,只能做成表吗?
- Mysql高级篇学习总结14:子查询优化、排序优化、GROUP BY优化、分页查询优化
- Graphical Hands-on Tutorial--ESP32 One-Key Network Configuration (Smartconfig, Airkiss)
- The use of DDR3 (Naive) in Xilinx VIVADO (1) to create an IP core
- *W3C* Standards Organization
- 强烈推荐一款优秀且通用的后台管理系统
- Events in August | 51CTO's 17th Anniversary Celebration, post a blog post to get gifts such as tea sets/notebooks/T-shirts!
- Leetcode brush questions - binary search tree related topics (98. Verify binary search tree, 235. The nearest common ancestor of binary search tree, 1038. From binary search tree to bigger sum tree, 5
- Business collocations
- Leetcode——利用先序遍历特性完成114. 二叉树展开为链表
猜你喜欢
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
3-5年以上的功能测试如何进阶自动化?
Leetcode刷题——路径总和
字节技术官亲码算法面试进阶神技太香了
iMeta | German National Cancer Center Gu Zuguang published a complex heatmap visualization method
Camunda overall architecture and related concepts
【Inspirational】The importance of review
C#/VB.NET:在 Word 中设置文本对齐方式
The use of DDR3 (Naive) in Xilinx VIVADO (2) Read and write design
What is the terminal privilege management
随机推荐
MySQL 45 讲 | 11 怎么给字符串字段加索引?
【LeetCode】899.有序队列
技术干货 | 用零信任保护代码安全
Meishe Q&A Room | Meiying VS Meishe Cloud Editing
【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
知网网站地址更换
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
mongo-导出数据到mysql
命令模式(Command)
Leetcode Brush Questions - Path Sum
北京大学,新迎3位副校长!其中一人为中科院院士!
『快速入门electron』之实现窗口拖拽
Digital management insight into retail and e-commerce operations - retail password
网管型交换机比傻瓜交换机多了哪些功能
【LeetCode】232.用栈实现队列
What is the terminal privilege management
Leetcode刷题——二叉搜索树相关题目(98. 验证二叉搜索树、235. 二叉搜索树的最近公共祖先、1038. 从二叉搜索树到更大和树、538. 把二叉搜索树转换为累加树)
cat /proc/kallsyms 发现内核符号表值都为0
超美星空特效,你Get了吗?
Use pytest hook function to realize automatic test result push enterprise WeChat