2019-02-12
閱讀量:
944
mysql in和exists的用法有什么異同?
問(wèn)題描述:
mysql查詢(xún)中,經(jīng)常使用到in函數(shù),那么和它具有類(lèi)似功能的exists在使用上有什么區(qū)別呢??jī)烧叩倪m用范圍又是什么?
解決思路:
create table test(score varchar(10));
insert test_new values('A'),('B');
create table test_new(score varchar(10));
insert test_new values('A'),('B'),('C'),('D');
- 如果要查詢(xún)test中和test_new相同的數(shù)據(jù),可以用in來(lái)表示
select * from test where score in (select score from test_new);
- 也可以用exists來(lái)表示
select * from test a where exists (select score from test_new where score=a.score);
- 要注意exists是緊跟著where語(yǔ)句的,沒(méi)有score限定字段
- exists表示的是(子查詢(xún))中的布爾值,運(yùn)行邏輯是先主查詢(xún)一次,再每一行執(zhí)行一次子查詢(xún),如果true則最后整合輸出
適用范圍來(lái)看,兩者功能接近,但是效率不同:
- 子查詢(xún)的表大用exist效率高
- 涉及not in 或者 not exists的時(shí)候,后者效率高






評(píng)論(0)


暫無(wú)數(shù)據(jù)
CDA考試動(dòng)態(tài)
CDA報(bào)考指南
推薦帖子
0條評(píng)論
1條評(píng)論
0條評(píng)論
0條評(píng)論