oracle个人记录

create table gao.tcparent(
p_id integer primary key not null,
p_name varchar2(10)
)




create table gao.tcchild(
c_id integer primary key not null,
f_id integer,
c_name varchar2(10)
)


--全连接
select * from gao.tcchild a full join  gao.tcparent b  on a.f_id=b.p_id


--内连接
select * from gao.tcchild a inner join  gao.tcparent b  on a.f_id=b.p_id
select * from gao.tcchild a,gao.tcparent b where a.f_id=b.p_id


--左连接,左边的记录tcchild记录都有,右边的记录tcparent可能有些为空 相对于 a表的左连接
select * from gao.tcchild a left join gao.tcparent b on a.f_id=b.p_id
select * from gao.tcchild a,gao.tcparent b where a.f_id=b.p_id(+)
--右连接,右边的记录都有,左边的记录可能有些为空
select * from gao.tcchild a right join gao.tcparent b on a.f_id=b.p_id
select * from gao.tcchild a,gao.tcparent b where a.f_id(+)=b.p_id
/**
左还是右,是与(+)在等号的哪边相反的
对于外连接, 也可以使用“(+) ”来表示。 关于使用(+)的一些注意事项:
       1.(+)操作符只能出现在where子句中,并且不能与outer join语法同时使用。
       2. 当使用(+)操作符执行外连接时,如果在where子句中包含有多个条件,则必须在所有条件中都包含(+)操作符
       3.(+)操作符只适用于列,而不能用在表达式上。
       4.(+)操作符不能与or和in操作符一起使用。
       5.(+)操作符只能用于实现左外连接和右外连接,而不能用于实现完全外连接。
**/       


---------------------------------------------------------------exists理解---------------------------------------------------------

--exists 以下这两个sql 等同
select * from gao.tcchild a where exists (select 1 from gao.tcparent b where b.p_id=a.f_id) 
select * from gao.tcchild  where f_id in  (select p_id from gao.tcparent ) 

--等同
select * from gao.tcchild
select * from gao.tcchild where exists (select 1 from dual where 1=1)
select * from gao.tcchild where exists(select null from dual)

在SQL中使用exists表示判断子查询是否存在记录,它返回的是true或false字段。如果有记录返回,则返回值,否则返回为false。

exists做为where 条件时,是先对where 前的主查询询进行查询,然后用主查询的结果一个一个的代入exists的查询进行判断,如果为真则输出当前这一条主查询的结果,否则不输出。(其实就是判断exists括号里面是否查询出来值,如果查询的出来就是返回true)当子查询与主表不存在关联关系时,简单认为只要exists为一个条件判断,如果为true,就输出所有记录。如果为false则不输出任何的记录。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值