Oracle基础开发
单表查询
简单条件查询
-- 精准查询
select * from 表名 where 字段 = '条件';
-- 模糊查询:% 表示多个字符,_表示占位符
select * from 表名 where 字段 like '%条件%';
-- and 运算符
select * from 表名 where 字段1 = '条件1' and 字段2 = '条件2';
-- or 运算符
select * from 表名 where 字段 = '条件1' or 字段 = '条件2';
-- and or 混合运算:and的运算级高于or
select * from 表名 where (字段 = '条件1' or 字段 = '条件2') and 字段 = '条件3';
-- 范围查询:>、>=、<=、<、!>、!<、<> between ··· and ······
select * from 表名 where 字段 <= '条件1' or 字段 >= '条件2';
-- 空值查询
select * from 表名 where 字段 is null;
select * from 表名 where 字段 is not null;
去重查询
select distinct 字段 from 表名;
排序查询
select * from 表名 order by 字段 (asc/desc);
伪列
-- ROWID:每行数据的物理地址
select rowid, t.* from 表名 t;
-- ROWNUM:结果集的行号
select rownum, t.* from 表名;
聚合函数
-- 求和函数:sum
-- 平均函数:avg
-- 最大值函数:max
-- 最小值函数:min
-- 统计函数:count
-- 分组函数:group by
-- 分组后的条件查询:group by ······ having ······
多表查询
交叉连接
-- cross join ··· on ···
select * from 表1 t crose join 表2 u;
简写: select * from 表1 t, 表2 u;
自然连接
-- natural join ··· on ···
select * from 表1 t natural join 表2 u;
内连接
-- inner join ··· on ···
select * from 表1 t inner join 表2 u on t.条件 = u.条件;
简写: select * from 表1 t, 表2 u where t.条件 = u.条件;
左外连接
-- left join ··· on ···
select * from 表1 t left join 表2 u on t.条件 = u.条件;
简写: select * from 表1 t, 表2 u where t.条件 = u.条件(+);
右外连接
-- right join ··· on ···
select * from 表1 t right join 表2 u on t.条件 = u.条件;
简写: select * from 表1 t, 表2 u where t.条件(+) = u.条件;
子查询
where 子查询
单行子查询
- 返回一条记录
- 单行操作符:>、>=、<=、<、!>、!<、<>。
多行子查询
- 返回多条记录
- 多行操作符:(not)in、any、all
from 子查询
多行子查询
select 子查询
单行子查询
分页查询
-- 三层嵌套
select * from
(select rownum r,t.* from
(select * from 表 order by 字段 desc) t
where rownum<=20 )
where r>10
单行函数
字符函数
不详述,参见:https://www.cnblogs.com/champaign/p/5882023.html#_label2
数值函数
不详述,参见:https://www.cnblogs.com/champaign/p/5882030.html
日期函数
sysdate 系统变量:表示当前日期和时间
不详述,参见:http://www.cnblogs.com/zlbx/p/4817910.html?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io
转换函数
不详述,参见:https://www.cnblogs.com/story1/p/8012235.html
函 数 | 描 述 |
---|---|
CHARTOROWID | 将字符转换到 rowid 类型 |
CONVERT | 转换一个字符节到另外一个字符节 |
HEXTORAW | 转换十六进制到 raw 类型 |
RAWTOHEX | 转换 raw 到十六进制 |
ROWIDTOCHAR | 转换 ROWID 到字符 |
TO_CHAR | 转换日期格式到字符串 |
TO_DATE | 按照指定的格式将字符串转换到日期型 |
TO_MULTIBYTE | 把单字节字符转换到多字节 |
TO_NUMBER | 将数字字串转换到数字 |
TO_SINGLE_BYTE | 转换多字节到单字节 |
其它函数
空值处理函数 NVL
NVL(检测的值,如果为 null 的值);
空值处理函数 NVL2
NVL2(检测的值,如果不为 null 的值,如果为 null 的值);
条件取值 decode
decode(条件,值 1,翻译值 1,值 2,翻译值 2,…值 n,翻译值 n,缺省值)
类似语法:
(case ··· when ··· then ··· else ··· end)
(case when ··· then ··· else ··· end)
行列转换
分析函数
RANK 相同的值排名相同,排名跳跃
DENSE_RANK 相同的值排名相同,排名连续
ROW_NUMBER 返回连续的排名,无论值是否相等
集合运算
关系 | 关键字 | 描述 |
---|---|---|
并集 | UNION ALL | 返回各个查询的所有记录,包括重复记录 |
并集 | UNION | 返回各个查询的所有记录,不包括重复记录 |
交集 | INTERSECT | 返回两个查询共有的记录 |
差集 | MINUS | 返回第一个查询检索出的记录减去第二个查询检索出的记录之后剩余的记录 |