数据库----函数

简介: 数据库----函数

函数介绍

函数是指一段可以直接被另一段程序调用的程序或代码

函数的分类

1.字符串函数

2.数值函数

3.日期函数

4.流程函数

字符串函数

# 字符串拼接
select concat( 'hello ', 'world' );
# 字符串全部转为小写
select lower( 'HELLO' );
# 字符串全部转为大写
select upper( 'hello' );
# 左填充
select lpad( '12', 5, '0' );
# 右填充
select rpad( '12', 5, '-' );
# 去除左右空格
select trim( '  hello world  ' );
# 获取子字符串
select substring( 'hello world', 1, 5 );

练习

# 将emp表中的workno统一为五位数,不足前面补0
update emp set workno = lpad( workno, 5, '0' );
select * from emp;

数值函数

# ceil
select ceil(1.1);  #结果  2
# floor
select floor(1.9); #  1
# mod
select mod( 5, 4 ); #  1
# rand
select rand(); #随机数
# round
select round(1.4); #  1
select round(1.5); #  2

练习

# 使用rpad,因为存在0.012345的情况,乘1000000只有五位整数,所以要补齐
select rpad( round( rand()*1000000 ), 6, '0' );

日期函数

# curdate当前日期
select curdate();
# curtime当前时间
select curtime();
# now当前日期时间
select now();
# year指定date的年
select year(now());
# month
select month(now());
# day
select day(now());
# date_add
select date_add(now(), interval 20 day );
select date_add(curdate(), interval 20 day );
# datediff 前大后小
select datediff('2023-11-11', now());  #  614

练习

select name,datediff(curdate(), entrydate) as 'entryDay' from emp order by entryDay desc ;

流程函数

# if
select if( true, 'true', 'error' );
select if( false, 'true', 'error' );
# ifnull
select ifnull('123', 'Null');
select ifnull(null , 'null');
# 查询表emp的员工姓名和工作地址(北京、上海--一线城市,其他--二线城市)
select
       name,
       (case workaddress when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end) '工作地址'
from emp;

导入表:

# 创建表
create table score (
    id int comment 'ID',
    name varchar(20),
    math int,
    english int,
    chinese int
);
# 添加数据
insert into
    score (id, name, math, english, chinese)
values
       (1,'tom',67,88,95),
       (2,'rose',23,66,90),
       (3,'jack',56,98,76);
select
       id,
       name,
       case when math>=85 then '优秀' when math>=60 then '及格' else '不及格' end as 'math',
       case when english>=85 then '优秀' when english>=60 then '及格' else '不及格' end as 'english',
       case when chinese>=85 then '优秀' when chinese>=60 then '及格' else '不及格' end as 'chinese'
from score;

相关文章
|
2月前
|
SQL 关系型数据库 MySQL
【MySQL 数据库】2、MySQL 的数据控制语言、函数和约束
【MySQL 数据库】2、MySQL 的数据控制语言、函数和约束
38 0
|
2月前
|
SQL 存储 关系型数据库
【MySQL技术专题】「实战开发系列」一同探索一下数据库的加解密函数开发实战指南之AES系列
【MySQL技术专题】「实战开发系列」一同探索一下数据库的加解密函数开发实战指南之AES系列
80 0
|
12天前
|
SQL 关系型数据库 MySQL
MySQL数据库基础第二篇(函数)
MySQL数据库基础第二篇(函数)
|
24天前
|
SQL 关系型数据库 MySQL
MySQL数据库——基础篇总结(概述、SQL、函数、约束、多表查询、事务)一
MySQL数据库——基础篇总结(概述、SQL、函数、约束、多表查询、事务)一
26 5
|
24天前
|
关系型数据库 MySQL 数据库
MySQL数据库——函数-字符串函数、数值函数、日期函数、流程函数
MySQL数据库——函数-字符串函数、数值函数、日期函数、流程函数
16 2
|
5天前
|
存储 传感器 时序数据库
时序数据库influx有字符串拼接函数吗
【6月更文挑战第25天】时序数据库influx有字符串拼接函数吗
9 0
|
7天前
|
SQL 分布式计算 MaxCompute
MaxCompute操作报错合集之通过UDF(用户定义函数)请求外部数据库资源并遇到报错,是什么原因
MaxCompute是阿里云提供的大规模离线数据处理服务,用于大数据分析、挖掘和报表生成等场景。在使用MaxCompute进行数据处理时,可能会遇到各种操作报错。以下是一些常见的MaxCompute操作报错及其可能的原因与解决措施的合集。
|
2月前
|
关系型数据库 MySQL Linux
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)
|
2月前
|
存储 Java 数据库
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数(二)
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数
55 0
|
24天前
|
存储 SQL 关系型数据库
MySQL数据库——存储函数(介绍、案例)
MySQL数据库——存储函数(介绍、案例)
40 0