Java小白的数据库爱情(三)Oracle 简单语法

Java小白的数据库爱情(三)Oracle 简单语法

简单查询

查询:select *|字段名1,字段名2|… from 数据源

取别名:结果集中的字段 表别名

​ “ ” -> 原封不动显示

		select 	字段名(as) 别名, 字段名   别名,...... from 数据源 	别名

字符串:’ ‘

字符串的拼接:||

伪列:表中不存在字段,但可以穿:表达式,数值,字符串

虚表:dual

null:null 值与数值参与运算还是null,与字符串拼接 结果为原串

​ 处理 : ->nvl(字段,值2) 当字段值不为null时,nvl函数的最终结果为字段值,如果字段值为null时,函数的最终结果为 函数的值2

条件查询

条件查询:select 数据 from 数据源 where 过滤条件

执行顺序:from—>where—>select

a)、= 、 >、 <、 >=、 <=、 !=、 <>、 between and
b)、and 、or、 not、 union、 union all、 intersect 、minus
c)、null :is null、 is not null、 not is null
d)、like :模糊查询 % _ escape(‘单个字符’)
f)、in 、 exists(难点) 及子查

between and :在…和…之间

select * from emp where sal between 1500 and 3000;

–union 并集(去重)、 union all并集不去重、 intersect交集 、minus 差集

select * from emp where sal>= 1500
intersect
select * from emp where  sal<=3000;

select deptno from dept
minus
select distinct deptno from emp;

模糊匹配:like

%:任意个任意字符 _:一个任意字符

%和_ 通常一起使用

in:相当于使用or的多个等值,定值集合 in (值1,值2…)

​ 如果记录多效率低, 用于一些 少量定值判断

exists: exists() 如果()条件为true,即存在记录则返回结果

order by : 排序 asc(默认) 升序 ; desc 降序

order by 字段1  asc (desc) ,字段2 asc (desc)
单行函数

sysdate|curtent_date 以date 类型返回当前的日期

add_months(d,x) 返回加上X个月后的日期d 的值

last_day (d) 返回的所在月份的最后一天

months between (date1 ,date2) 返回date1 和date2 之间月的数目

next_day( sysdate ,星期一) 下一个星期一

date类型的日期可直接做加减

to_char (d,m) —>将日期以指定的格式转换为字符串

to_date(c,m)–>将字符串以指定格式转换为日期

其他函数

nvl : nvl(string1,string2) 如果string1为 null,则结果为string2的值

decode: decode (判定字段 ,值1,结果1,值2,结果2… 默认值)

select ename,sal,deptno,decode(deptno,20 ,sal*1.1) "新工资" from emp;

case when then else end

select deptno case deptno when 10 then sal * 1.1 when 20 then sal * 1.08 when 30 then sal * 1.15 else sal * 1.2end) raisesal  from emp;
多行|聚合|组函数

count (1|*|字段名) max() min() avg() sum()

selcect 后面可以多个组函数同时使用

先确定要计算的结果集|组|多条数据,然后再进行组函数计算

注意:

在select后面,组函数只能和其他组函数,或者分组字段一起使用

null值不参与组函数的计算

分组 group by 分组字段

子查询 将查询结果作为条件进行查询

分组

group by

定义:把根据不同的逻辑分成不同的小组,以小组为单位进行计算

如果使用分组,select后面的只能使用分组字段或者组函数

having :对分组进行筛选

语句:select 数据 from 数据源 where 过滤条件 group by 分组字段 having 组过滤条件 order by 排序字段

执行流程:from where group by having select order by

注意: where后面不能使用组函数,having后可

在where和having的后面都不能使用别名

--先过滤后分组
select max(sal),deptno from emp where deptno in (20,30) group by deptno;
--先分组后过滤
select max(sal),deptno from emp group by deptno having deptno in(20,30);·
行转列
select name , decode(course,'语文',score), decode(course,'数学',score),  decode(course,'英语',score) from tb_student;

select name,
       max(decode(course, '语文', score)) 语文,
       min(decode(course, '数学', score)) 数学,
       max(decode(course, '英语', score)) 英语
  from tb_student
 group by name;
表连接

分为 92语法 和 99语法

92语法

内链接:

​ select 数据 from 数据源1,数据源2,…

​ 笛卡尔积 对乘

​ 多个数据源中的同名字段需要指明出处

​ 等值连接 : 表连接的时候,满足等值连接条件的菜保留

select * from emp e,dept  d where e.deptno = d.deptno;
select * from emp e,dept  d where e.deptno = d.deptno and e.deptno in(20,30) and e.sal>1500;

​ 非等值连接: 连接条件不是=判断

select * from salgrade where 1500 between losal and hisal;

外连接

外连接 :
主表 : 主表中的数据无论是否满足连接条件全都显示
定义一张表为主表: 92语法中,连接条件位置,主表对面添加(+)
做外连接|左连接: 主表在,的左边叫做左连接
右外连接|右连接: 主表在,的右边叫做右连接

查询所有员工信息以及这个员工的上级经理人信息
主表: 员工表

select * from emp e2,emp e1 where e1.mgr = e2.empno(+);
select * from emp e1,emp e2 where e1.mgr(+) = e2.empno;

99语法

​ 笛卡尔积 cross join

​ 自然连接 natural join 把两张表中的同名字段|主外键关系的字段 自动做等值(值相等=)
​ 自然连接使用同名字段不能使用限定词

​ 使用某一个同名字段做等值连接 join … using(字段名)

select * from emp join dept using(deptno);

join on 连接 -->等值连接 非等值 自连接 (解决 一切) 关系列必须区分

--on 
select ename, dname  
  from emp  
  join dept    
	on emp.deptno = dept.deptno 
  where emp.deptno = 30;

left outer join …on 左连接

right outer join …on 右连接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值