1. 函数 1.1 大小写转换函数 函数 描述 LOWER() 全小写 UPPER() 全大写 INITCAP() 首字母大写 1.2 字符串操作函数 函数 描述 concat() 拼接字
函数 | 描述 |
---|---|
LOWER() | 全小写 |
UPPER() | 全大写 |
INITCAP() | 首字母大写 |
函数 | 描述 |
---|---|
concat() | 拼接字符串 |
substr(字段,startIndex,endIndex) | 截取字符串 |
length() | 字符串长度 |
instr(字段,"字符") | 指定字符出现的位置索引 |
trim() | 取出指定字符前后的空格 |
函数 | 描述 |
---|---|
round(参数,保留几位) | 向上取整 |
trunc(参数,保留几位) | 直接取整,不四舍五入 |
mod(x,y) | x除以y的余数 |
函数 | 描述 |
---|---|
add_months(date,n) | 在日期date上加上一个n月 |
lastday(date) | 返回指定日期当前月的最后一天 |
round(date,[fmt]) | 返回一个一fmt为格式的四舍五入 |
trunc(date,[fmt]) | 不对日期进行舍入,直接进行截取 |
extract(fmt from date) | 提取日期中的特定部分 |
sysdate | 返回当前系统时间 |
函数 | 描述 |
---|---|
to_char(date | number,[ fmt ]) | 把日期或字符串转为特定的字符串 |
to_date(x,[ fmt ]) | 把一个字符串以fmt转换成一个日期类型 |
to_number(x,[ fmt ]) | 把一个字符串以fmt格式转换成一个数字 |
函数 | 描述 |
---|---|
nvl(x,value) | 如果x为空,返回value,否则返回x |
nvl2(x,value1,value2) | 如果x为空,返回value1,否则返回value2 |
函数 | 描述 |
---|---|
avg() | 平局值 |
sum() | 求和 |
min() | 最大值 |
max() | 最小值 |
count() | 计数 |
case...when...then...else...end: 相当于 if...else if...else
-- 写法一: 直接跟字段
case 字段 when 值1 then 结果1
when 值2 then 结果2
else 结果三 end
-- 写法二: 跟判断语句
case when 条件判断1 then 结果1
when 条件判断2 then 结果2
else 结果3 end
decode:
decode(字段1,"值1",结果1,"值2",结果2,结果3)
方法 | 描述 |
---|---|
UNION | 取出重复记录 |
UNioN ALL | 保留重复记录 |
INTERSECT | 取交集 |
MINUS | 取差集 |
语法:
-- 相当于左外连接
select * from a,b where a.id = b.id(+)
-- 相当于右外连接
select * from a,b where a.id(+) = b.id
相当于Mysql中的自动增长序列
完整写法:
创建序列:
查询序列:
删除序列:
提高检索的速度
-- 创建索引:
create index 索引名 on 表名(列)
-- 删除索引:
drop index 索引名
可以简单理解为分组后的展示所有数据
row_number()over( partition by "分区字段" order by "分组字段" asc/desc )
drop table emp;
create table emp(
eid number,
ename varchar2(224),
birthday date,
salary float,
did number
)
insert into emp values(1,"jack",to_date("2000-1-30 16:20:31","yyyy-mm-dd hh24:mi:ss"),3000.0,2);
insert into emp values(2,"rose",to_date("1999-5-1 17:00:00","yyyy-mm-dd hh24:mi:ss"),5000.0,3);
insert into emp values(3,"admin",to_date("2000-10-1 00:56:59","yyyy-mm-dd hh24:mi:ss"),4000.0,4);
insert into emp values(4,"zhangsan",to_date("1998-1-1 12:30:00","yyyy-mm-dd hh24:mi:ss"),4500.0,1);
insert into emp values(5,"lisi",to_date("1900-4-1 14:07:20","yyyy-mm-dd hh24:mi:ss"),55000.0,null);
drop table dept;
create table dept(
did number,
dname varchar2(225)
)
insert into dept values(1,"管理");
insert into dept values(2,"运维");
insert into dept values(3,"人事");
insert into dept values(4,"开发");
-- 字符串处理函数
-- 转换大写 upper
select upper("aaa") from emp;
-- 转换小写 lower
select lower("AAA") from emp;
-- 首字母大写 initcap
select initcap("hello Word!!!") from emp;
-- 字符串截取 substr
select substr("helloword!!!",0,5) from emp;
-- 字符串替换 replace
select replace("helloword!!!","!","?") from emp;
-- 字符串长度 length
select length("helloword!!!") from emp;
-- 指定字符出现的位置 instr
select instr("hellowword!!!","h") from emp;
-- 数值函数
-- 四舍五入 round
select round(15.66,-2) from emp; -- 0
select round(15.66,-1) from emp; -- 20
select round(15.66,0) from emp; -- 16
select round(15.66,1) from emp; -- 15.7
select round(15.66,2) from emp; -- 15.66
-- 截取 trunc
select trunc(15.66,-2) from emp; -- 0
select trunc(15.66,-1) from emp; -- 10
select trunc(15.66,0) from emp; -- 15
select trunc(15.66,1) from emp; -- 15.6
select trunc(15.66,2) from emp; -- 15.66
-- 取余数 mod
select mod(20,3) from emp;
-- 日期函数
-- 查询系统时间 sysdate
select sysdate from emp;
-- 查询今年多少岁
select ename,(sysdate - birthday)/365 from emp;
-- 查询过了多少月 months_between
select ename,months_between(sysdate,birthday) from emp;
-- 查询三个月后的日期 add_months
select ename,birthday,add_months(birthday,3) from emp;
-- 转换函数
-- 把当前时间转换成指定格式的字符串 to_char fm/去零
select to_char(sysdate,"yyyyfm/mm/dd hh24:mi:ss") from emp;
-- 将数字转换成字符串
select to_char(99) from emp;
-- 显示成年月日
select to_char(sysdate,"yyyy") || "年" || to_char(sysdate,"mm") || "月" || to_char(sysdate,"dd") || "日" from emp;
-- 把字符串转换成日期类型 to_date
select to_date("1999-10-1 15:24:31","yyyy/mm/dd hh24:mi:ss") from emp;
-- 将字符串转换成数字 to_number
select to_number("99") from emp;
-- 通用函数
-- 空值处理函数 nvl /如果为null,返回指定字符
select nvl(ename,"无") from emp;
-- 空值处理函数 nv2 /如果buweinull,返回指定字符1/为null,返回指定字符2
select nvl2(ename,"有","无") from emp;
-- 条件表达式
-- 将指定内容转换成"xxx" decode
select decode(ename,"admin","管理员","其他") from emp;
-- 其他方式 case when then end
select case ename when "admin" then "管理员" else "其他" end from emp;
-- 查询工资最高的三个人
-- rownum: 给结果集加上一个序列号
select rownum,e.* from (select * from emp order by salary desc) e where rownum <= 3;
select * from (select rownum r,e1.* from (select * from emp order by salary desc) e1 ) e2
where e2.r > 3 and e2.r <= 6;
-- 集合函数
-- 交集 intersect/相当于and
select * from emp where ename = "admin"
intersect
select * from emp where salary = 4000.0;
-- 并集 union/相当于or
select * from emp where ename = "admin"
union
select * from emp where salary = 5000;
-- 差集 minus/第一个结果集减去第二个结果集
select * from emp where ename = "admin"
minus
select * from emp where salary = 4000;
-- exists/如果存在返回true/反之false
-- 找出哪一个没有部门信息
select * from emp where exists(select * from dept where emp.did = dept.did)
select * from emp where not exists(select * from dept where emp.did = dept.did)
-- 序列 相当于mysql中的自动增长序列,操作失败也会增长
-- 创建序列
create sequence emp_seq;
-- 查询序列
select emp_seq.nextval from emp;
-- 删除序列
drop sequence emp_seq;
-- 索引
-- 创建索引
create index index_ename on emp(ename)
-- 删除索引
drop index index_ename;
--结束END--
本文标题: 01. Oracle(基础语法)
本文链接: https://www.lsjlt.com/news/5680.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0