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

SQL学习笔记3

SQL常用函数

1、字符串函数

函数调用的语法:select 函数(参数);

常用的字符串函数有:

拼接字符串,将几个字符串拼到一起:concat (s1,s2,……);

select concat('你好','hello');
update mytable set wherefo = concat('中国',wherefo);

将所有字符转换为大写:upper(字符串);

select upper('hello')

将所有字符转换为小写::lower(字符串);

select lower('HELLO');

使用指定的字符对字符串左填充到指定长度:lpad(字符串,长度,填充字符);

select lpad('01',5,'*');
update mytable set number= lpad(number,10,'0');

使用指定的字符对字符串右填充到指定长度:rpad(字符串,长度,填充字符);

select rpad('01',5,'*');

去除字符串头尾的空格:trim(字符串)

select trim('  hello  ');

返回从指定位置出发长度为n的字符串子串:substring(string,start,len);

select substring('hellohello',1,5);
update mytable set number = substring(number,5,6)

2、数值函数

常见的数值函数有

对x向上取整,取大于x的最小整数:ceil(x)

select ceil(1.2);

对x向下取整,取小于x的最大整数:floor(x)

select floor(1.9);

取x/y的模,x除以y的余数:mod(x,y)

select mod(5,4);

生成0-1之间的随机数:rand()

#随机生成六位数验证码
select lpad((round(rand(),6))*1000000,6,(round(rand(),1)*10));

取x四舍五入后保留y位的数:round(x,y)

select round(3.14159,2)

3、日期函数

返回当前日期:curdate()

select curdate();

返回当前时间:curtime()

select curtime();

返回当前日期与时间:now()

select now();

获取指定date的年份:year(date)

select year('2025-3-3');

获取指定date的月份:month(date)

select month('2025-3-3');

获取指定date的日期:day(date)

select day('2025-3-3');

返回date加指定时间exp的类型后的date:date_add(date,interval exp type)

select date_add('2025-3-3',interval 70 day);

返回date1与date2相差的时间:datediff(date1,date2)

select datediff(curdate(),'2025-3-3')
select name as ‘姓名’,datediff(curdate(),dates) as '入学天数' from mytable order by '入学天数','姓名';

4、流程函数

判断value值,若为ture,则返回值1,若为false,则返回值2:if(value,值1,值2)

select name as '姓名' ,if(wherefo='中国北京','是','否') as '是否北京' from mytable;

判断值1的值,若值1不为null,则返回值1,否则返回值2:ifnull(值1,值2)

select ifnull(null,1);

若value1为ture,则返回值1,若value2为ture,则返回值2……否则返回default:case when value1 then 值1 when  value2 then 值2 ……else default end

selectname as '姓名',(case when score >=560 then '优秀'when score >=520 then '良好'when score >=480 then '及格'else '不及格'end) as '成绩情况'
from mytable;

判断exp的值,若为value1,则返回值1……否则返回default:case exp when value1 then 值1……else default end

select name as '姓名',(case wherefowhen '中国北京' then '一线城市'when  '中国上海' then '一线城市'else '其他城市'end) as '居住情况'
from mytable;

http://www.xdnf.cn/news/14646.html

相关文章:

  • 图像质量对比感悟
  • 智表ZCELL产品V3.2 版发布,新增拖动调整行列功能,修复了插件引用相对路径等问题
  • 【C++11】右值引用和移动语义
  • Hive3.1.3加载paimon-hive-connector-3.1-1.1.1.jar报错UnsatisfiedLinkError
  • 解决uniapp vue3版本封装组件后:deep()样式穿透不生效的问题
  • 【攻防篇】解决:阿里云docker 容器中自动启动xmrig挖矿
  • 超实用AI工具分享——ViiTor AI视频配音功能教程(附图文)
  • php项目部署----------酒店项目
  • 知攻善防应急靶机 Windows web 3
  • LVS-DR负载均衡群集深度实践:高性能架构设计与排障指南
  • 笔记02:布线-差分对的设置与添加
  • Liunx操作系统笔记2
  • 《解锁前端潜力:自动化流程搭建秘籍》
  • Boosting:从理论到实践——集成学习中的偏差征服者
  • linux-修改文件命令(补充)
  • Jenkins Pipeline 与 Python 脚本之间使用环境变量通信
  • 数的三次方根
  • 【深度学习新浪潮】空间计算的医疗应用技术分析(简要版)
  • TCP/UDP协议深度解析(二):TCP连接管理全解,三次握手四次挥手的完整流程
  • Linux docker拉取镜像报错解决
  • 空间理解模型 SpatialLM 正式发布首份技术报告
  • 数据结构 顺序表与链表
  • 一步部署APache编译安装脚本
  • 基于SSM框架+mysql实现的监考安排管理系统[含源码+数据库+项目开发技术手册]
  • 使用VIVADO合并FPGA bit文件和Microblaze elf
  • SQL学习笔记2
  • 【大厂机试题解法笔记】可以组成网络的服务器
  • 使用亮数据网页抓取API自动获取Tiktok数据
  • Windows下安装zookeeper
  • 使用OpenCV实现中文字体渲染与特效处理