当前位置: 首页 > news >正文

SQL-函数

1、字符串函数

# 字符函数
select concat('hello' , ' mysql!');
select lower('HELLO');
select upper('hello');
select lpad('01',5,'-');# 左填充
select rpad('01',5,'-');# 右填充
select trim('  hello mysql ! ');# 去除前后空格
select substring('hello mysql!',1,7);# 截取一部分字符前7个,索引从1开始

#  补充工号为5位,前面补充0
desc employee;
select * from employee;
update employee set workno = lpad(workno,5,'0') where workno<10000;

2、数值函数

# 数值函数
select ceil(1.7); # 向上取整
select floor(1.1); # 向下取整
select mod(7,4); # 求余数
select rand(); # 0-1之间随机数
select round(2.35677,2); # 四舍五入,保留两位

# 生成一位六位数的随机验证码
select lpad(round(rand()*1000000,0),6,'0');

3、日期函数

# 日期函数
select curdate();# 当前日期  2024-08-27
select curtime(); # 当前时间
select  now(); # 当前日期和时间 2024-08-27 09:10:02
select year(now()); # 年
select month(now());# 月
select day(now());#日
select date_add(now(),interval 7 day); #2024-09-03 09:09:48
select datediff('2024-09-01','2024-10-02');# 返回相差的天数


# 查询全部人的入职天数,并根据天数倒序排序
select * from employee;
update employee set entrydate='2019-09-07' where workno ='00066';
update employee set entrydate='2019-09-08' where workno ='00067';

select  name,workno,datediff(curtime(),entrydate) days from employee order by days desc;
select  name,workno,datediff(curtime(),entrydate) as 'entrydays' from employee order by entrydays desc;

4、流程函数

# 流程函数
select if(true,'ok','error'); # 第一个参数条件语句true则返回第二个参数,否则第三个
select if(1=2,'ok','error');
select if(1=1,'ok','error');
select  ifnull('ok','Default');# 判断第一个参数是否为空,非空返回第一个,否则第二个
select  ifnull(null,'Default');


alter table employee add ip varchar(10) comment '地址';
update employee set ip ='北京' where workno=66;
update employee set ip ='武汉' where workno=67;
# 查询姓名和地址(北京---->政治中心,其他------->金融中心)
select
    name,
    (case ip
        when '北京' then '政治中心'
        when '上海' then '金融中心'
        else '其他'
        end) as 'IP属性'
from employee
order by IP属性 desc;




# 统计成绩给出等级
create table score(
    id int comment 'id',
    name varchar(5) comment '姓名',
    math int comment'数学',
    english int comment '英语',
    chinese int comment'语文'
)comment'学院成绩表';
insert into score (id,name,math,english,chinese)
values(1,'tom',67,46,36),(2,'jim',23,56,78),(3,'alice',78,99,65),(4,'momo',67,87,75);
select
    id,
    (case when math>=85 then 'A'when math>=60 then 'B' else 'C' end) '数学',
    (case when english>=85 then 'A'when english>=60 then 'B' else 'C' end) '英语',
    (case when chinese>=85 then 'A'when chinese>=60 then 'B' else 'C' end) '语文'
from score;

相关文章:

  • HTML实现俄罗斯方块
  • 2024/8/30 英语每日一段
  • HTML <template> 标签的基本技巧
  • 一维/二维高斯分布的负对数似然推导
  • 黑客声称窃取海量腾讯数据?数据库加密看这...
  • 零基础Opencv学习(三)
  • 制造业中的MES知识与ERP\PLM\WMS架构关系(附智能制造MES解决方案PPT下载)
  • Oracle 和 PostgreSQL 主从对比介绍
  • 【服务对接】✈️SpringBoot 项目整合华为云 obs 对象存储服务
  • Codeforces Round 969 (Div. 2)
  • npm install速度慢,install超时报错----npm换源;npm i npm install区别
  • 使用Python写贪吃蛇游戏
  • Golang | Leetcode Golang题解之第385题迷你语法分析器
  • 图形化编程系统学习10
  • 如何在知行之桥上通过业务单号查找原始报文?
  • erlang学习:用OTP构建系统3,应用程序服务器
  • SQL视图:简化复杂查询的利器
  • python——python-docx
  • ffmpeg视频转切片m3u8并加密videojs播放hls.js播放dplayer播放(弹幕效果)
  • Mysql集群
  • 山西太原一处居民小区发生爆炸,现场产生大量浓烟
  • 广东省副省长刘红兵任湖南省委常委、宣传部部长
  • 大型长读长RNA测序数据集发布,有助制定精准诊疗策略
  • 北京银行一季度净赚超76亿降逾2%,不良贷款率微降
  • 人社部:一季度全国城镇新增就业308万人,同比增加5万人
  • 铁路五一假期运输今日启动,预计发送旅客1.44亿人次