SQL中exists、 not exists語法及案例練習

目錄
一、exists語法
二、not exists 語法
三、exists案例練習 (重點)
四、not exists案例練習 (重點)
五、練習案例使用數據源

前言:文章對於exists、not exists底層邏輯沒做詳細的解釋,僅介紹了執行結果。如果想學習優化的同學可以看其他文章,如果僅想了解exists、 not exists用法可看本文。
其實關於exists、not exists的學習第一步還是要知道它們的返回結果是什麼!要能看懂別人寫的SQL,第二步纔是關注優化原理。

一、exists語法

用exists代替in是SQL性能優化的一個手段,使用exists能提高查詢性能。

select  *  from tableex a
where exists
(select  *  from  tableln b where a.Bname=b.ANAME)
上面SQL返回結果是什麼?

關於EXISTS函數很早就知道了,但對裏面的邏輯一直不清楚。比如看下面這個SQL,不知道返回結果是tableex所有數據,還是返回tableex與tableln能關聯上的數據。
自己練習之後,知道返回結果是tableex與tableln 能關聯上數據

工作中用exists寫的案例

select 
from t1_gics_lcgrpcont a where a.cvalidate bitween date'2020-02-03' and date'2020-02-25'
and exists(select *  from t1_gis_lcgrpcontstate where a.grpcontno=b.grpcontno and b.edortype in('CT','WT'))

二、not exists 語法

not exists執行結果與not in 執行結果是一樣的,我們先看一個案例。

select  *  from tableln a
where not exists
(select  *  from tableex where a.aname=bname)
上面SQL返回結果是什麼?

返回tableln與tableex 不能關聯上的數據。

如果不是很瞭解的小夥伴可以練習下面的數據

三、exists案例練習

1、exists練習題:用exits和in語法查詢tablee表中的數據,並且tablee表中的Bname姓名在tableln中
select  *  from tableex where Bname in(select   ANAME from  tableln)
go
select  *  from tableex a
where exists
(select  *  from  tableln b where a.Bname=b.ANAME)

查詢結果如下

2、exists練習題:執行下面SQL語法,分析執行結果(必看!!)

建議放在數據庫執行下


select * from tableln a where exists(select * from tableex  where A.aname=bname);---返回A表中A.aname與B.bname能關聯上的數據
select * from tableln a where exists(select bid from tableex ); ---返回A表所有數據
select * from tableln a where exists(select * from tableex where bID=1);---返回A表所有數據
select * from tableln A where exists(select * from tableex B where B.bID=2);  ---返回A表所有數據
select * from tableln A where exists(select * from tableex B where B.bID=3); ---返回A表所有數據
select * from tableln A where exists(select * from tableex B where A.AID=1);---返回A表中AID=1的數據
select * from tableln A where exists(select * from tableex B where A.AID=2);---返回A表中AID=2的數據
select * from tableln A where exists(select * from tableex B where A.AID=3);---返回A表中AID=3的數據

四、not exists案例練習

1、not exists練習題:用not exits和not in語法查詢tableln 表中的數據,並且tableln 表中的aname姓名不在tableex 中
#not in
select  *  from tableln
where aname not in(select  bname  from tableex);

#not exists
select  *  from tableln a
where not exists
(select  *  from tableex where a.aname=bname);
2、not exists練習題:執行下面SQL語法,分析執行結果(必看!!)

建議放在數據庫執行下


select * from tableln A where not exists(select * from tableex B where A.aname=B.bname);---返回A表中A.aname與B.bname不能關聯上的數據
select * from tableln A where not exists(select * from tableex B where B.bID=1);---A表不返回任何數據
select * from tableln A where not exists(select * from tableex B where B.bID=2);  ---A表不返回任何數據
select * from tableln A where not exists(select * from tableex B where B.bID=3); ---A表不返回任何數據
select * from tableln A where not exists(select * from tableex B where A.AID=1);---返回A表中AID<>1的數據
select * from tableln A where not exists(select * from tableex B where A.AID=2);---返回A表中AID<>2的數據
select * from tableln A where not exists(select * from tableex B where A.AID=3);---返回A表中AID<>3的數據

五、案例使用數據源

#建表
create  table tableln
(aid varchar2(20),
aname varchar2(20),
asex varchar2(20));

create table tableex
(bid varchar2(20),
bname varchar(20),
bsex varchar2(20),
baddress varchar2(20));

#插入數據
insert into tableln
select 1 aa,'張晉娟' bb,'女' cc from dual union all
select 2 aa,'張翠蘭' bb,'女' cc from dual union all
select 3 aa,'李海濱' bb,'男' cc from dual union all
select 4 aa,'馬豔豔' bb,'女' cc from dual union all
select 5 aa,'鄧事文' bb,'男' cc from dual;

insert into tableex
select '1' aa,'馬豔豔' bb,'女' cc,'太原' dd from dual union all
select '2' aa,'譚建軍' bb,'男' cc,'長沙' dd from dual union all
select '3' aa,'李紅軍' bb,'男' cc,'長沙' dd from dual union all
select '4' aa,'丁秀娟' bb,'女' cc,'北京' dd from dual union all
select '5' aa,'鄧事文' bb,'男' cc,'深圳' dd from dual ;

總結:
1、看了19年寫的文章,知道爲什麼進步比別人快一點了。每次在工作中遇見不會的函數或者比較難的函數週末會詳細的總結成文章。
2、遇見一位同事每次提起一個知識點,他都說會。但是在實際運用中很慢甚至不會用。這就是區別吧,可能別人是瞭解或者聽過某個函數或者知識點,但是不會熟練的使用;而我能詳細的講解給別人。
3、所以針對工作常用的還是得熟練掌握,一個問題一個問題解決。
4、後期會更新not exists與exists優化原理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章