Mysql exists和in

來源:互聯網
上載者:User

標籤:mysql   name   nbsp   soft   loop   select   pre   擷取   get   

今天在學習sql語句時,遇到關於exsits的用法,下面是題目:

表架構

Student(S#,Sname,Sage,Ssex) 學生表 
Course(C#,Cname,T#) 課程表 
SC(S#,C#,score) 成績表 
Teacher(T#,Tname) 教師表

問題:

查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名:

  即執行主句查詢前,先查詢是否子句是否為真,若存在學過002課程的學生,再執行查詢學過001的,

此時查詢出來的結果,就符合既學過001又學過002。

select Student.`S#`,Student.Sname from Student,SC where Student.`S#`=SC.`S#` and SC.`C#`=‘001‘and exists(
Select * from SC as SC_2 where SC_2.`S#`=SC.`S#` and SC_2.`C#`=‘002‘);
由此對比in和exsits的用法:exsits:

exsits子句返回的並非查詢結果,而是布爾值,TRUE或者FALSE,exists對外表用loop逐條查詢,

每次查詢都會查看exists的條件陳述式,當 exists裡的條件陳述式,能夠返回記錄行時(無論記錄行是的多少,只要能返回),條件就為真

返回當前loop到的這條記錄,反之如果exists裡的條件陳述式不能返回記錄行,則當前loop到的這條記錄被丟棄,exists的條件就像一個bool條件,

當能返回結果集則為true,不能返回結果集則為 false。

In:

 in查詢相當於多個or條件的疊加,這個比較好理解,比如下面的查詢:

select * from user where userId in (1, 2, 3);

等效於

select * from user where userId = 1 or userId = 2 or userId = 3;

not in與in相反,如下

select * from user where userId not in (1, 2, 3);

等效於

select * from user where userId != 1 and userId != 2 and userId != 3;

總的來說,in查詢就是先將子查詢條件的記錄全都查出來,假設結果集為B,共有m條記錄,然後在將子查詢條件的結果集分解成m個,再進行m次查詢

值得一提的是,in查詢的子條件返回結果必須只有一個欄位,例如

select * from user where userId in (select id from B);

而不能是

select * from user where userId in (select id, age from B);

而exists就沒有這個限制

 

效能方面對比:

1: select * from A where exists (select * from B where B.id = A.id);

2: select * from A where A.id in (select id from B);

查詢1.可以轉化以下虛擬碼,便於理解

for ($i = 0; $i < count(A); $i++) {

  $a = get_record(A, $i); #從A表逐條擷取記錄

  if (B.id = $a[id]) #如果子條件成立

    $result[] = $a;

}

return $result;

大概就是這麼個意思,其實可以看到,查詢1主要是用到了B表的索引,A表如何對查詢的效率影響應該不大

假設B表的所有id為1,2,3,查詢2可以轉換為

select * from A where A.id = 1 or A.id = 2 or A.id = 3;

這個好理解了,這裡主要是用到了A的索引,B表如何對查詢影響不大。

Mysql exists和in

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.