Mysql子查詢例項

2021-06-04 02:44:53 字數 3375 閱讀 9500

一,子選擇基本用法

1,子選擇的定義

子迭擇允許把乙個查詢巢狀在另乙個查詢當中。比如說:乙個考試記分專案把考試事件分為考試(t)和測驗(q)兩種情形。下面這個查詢就能只找出學生們的考試成績

select * from score where event_id in (select event_id from event where type='t');

2,子選擇的用法(3種)

 用子選擇來生成乙個參考值

在這種情況下,用內層的查詢語句來檢索出乙個資料值,然後把這個資料值用在外層查詢語句的比較操作中。比如說,如果要查詢表中學生們在某一天的測驗成績,就應該使用乙個內層查詢先找到這一天的測驗的事件號,然後在外層查詢語句中用這個事件號在成績表裡面找到學生們的分數記錄。具體語句為:

select * from score where

id=(select event_id from event where date='2002-03-21' and type='q');

需要注意的是:在應用這種內層查詢的結果主要是用來進行比較操作的分法時,內層查詢應該只有乙個輸出結果才對。看例子,如果想知道哪個美國**的生日最小,構造下列查詢

select * from president where birth=min(birth)

這個查詢是錯的!因為mysql不允許在子句裡面使用統計函式!min()函式應該有乙個確定的引數才能工作!所以我們改用子選擇:

select * from president where birht=(select min(birth) from presidnet);

 exists 和 not exists 子選擇

上一種用法是把查間結果由內層傳向外層、本類用法則相反,把外層查詢的結果傳遞給內層。看外部查詢的結果是否滿足內部查間的匹配徑件。這種「由外到內」的子迭擇用法非常適合用來檢索某個資料表在另外乙個資料表裡面有設有匹配的記錄

資料表t1 資料表t2

i1 c1 i2 c2

1 2

3 a

c 2

3 4 c

a 先找兩個表內都存在的資料

select i1 from t1 where exists(select * from t2 where t1.i1=t2.i2);

再找t1表內存在,t2表內不存在的資料

select i1 form t1 where not exists(select * from t2 where t1.i1=t2.i2);

 in 和not in 子選擇

在這種子選擇裡面,內層查詢語句應該僅僅返回乙個資料列,這個資料列裡的值將由外層查詢語句中的比較操作來進行求值。還是以上題為例

先找兩個表內都存在的資料

select i1 from t1 where i1 in (select i2 from t2);

再找t1表內存在,t2表內不存在的資料

select i1 form t1 where i1 not in (select i2 from t2);

好象這種語句更容易讓人理解,再來個例子

比如你想找到所有居住在a和b的學生。

select * from student where state in(『a','b')

二, 把子選擇查詢改寫為關聯查詢的方法。

1,匹配型子選擇查詢的改寫

下例從score資料表裡面把學生們在考試事件(t)中的成績(不包括測驗成績!)查詢出來。

select * from score where event_id in (select event_id from event where type='t');

可見,內層查詢找出所有的考試事件,外層查詢再利用這些考試事件搞到學生們的成績。

這個子查詢可以被改寫為乙個簡單的關聯查詢:

select score.* from score, event where score.event_id=event.event_id and event.event_id='t';

下例可以用來找出所有女學生的成績。

select * from score where student_id in (select student_id form student where *** = 『f');

可以把它轉換成乙個如下所示的關聯查詢:

select * from score

where student _id =student.student_id and student.*** ='f';

把匹配型子選擇查詢改寫為乙個關聯查詢是有規律可循的。下面這種形式的子選擇查詢:

select * from tablel

where column1 in (select column2a from table2 where column2b = value);

可以轉換為乙個如下所示的關聯查詢:

select tablel. * from tablel,table2

where table.column1 = table2.column2a and table2.column2b = value;

(2)非匹配(即缺失)型子選擇查詢的改寫

子選擇查詢的另一種常見用途是查詢在某個資料表裡有、但在另乙個資料表裡卻沒有的東西。正如前面看到的那樣,這種「在某個資料表裡有、在另乙個資料表裡沒有」的說法通常都暗示著可以用乙個left join 來解決這個問題。請看下面這個子選擇查詢,它可以把沒有出現在absence資料表裡的學生(也就是那些從未缺過勤的學生)給查出來:

select * from student

where student_id not in (select student_id from absence);

這個子選擇查詢可以改寫如下所示的left join 查詢:

select student. *

from student left join absence on student.student_id =absence.student_id

where absence.student_id is null;

把非匹配型子選擇查詢改寫為關聯查詢是有規律可循的。下面這種形式的子選擇查詢:

select * from tablel

where column1 not in (select column2 from table2);

可以轉換為乙個如下所示的關聯查詢:

select tablel . *

from tablel left join table2 on tablel.column1=table2.column2

where table2.column2 is null;

注意:這種改寫要求資料列table2.column2宣告為not null。

MySQL子查詢操作例項詳解

定義兩個表tb1和tb2 create table tbl1 num1 int not null create table tbl2 num2 int not null 向兩個表中插入資料 insert into tbl1 values 1 5 13 27 insert into tbl2 valu...

mysql求和 子查詢 MySQL子查詢

到現在為止,我們已經表明,以select宣告是乙個簡單的查詢。該單個語句從單個資料庫表中檢索資料。sql還同意建立乙個子查詢。即巢狀在其他查詢的查詢。下列實施例給出巢狀查詢。一種表示訂單資訊儲存,包含訂單號,客戶id。訂購日期。例如以下所看到的 一張表示儲存訂單物品資訊,例如以下 另一張表儲存的是客...

mysql from 子查詢 mysql 子查詢

簡單的解釋下幾個資料庫概念以幫助理解子查詢的內容 1 什麼是建標 答 就是宣告列的過程。2 什麼是列答 列可以理解為變數,可以運算3 什麼是取出結果 答 可以理解為零時表 接下來建立2個表,email表和person表,例子的原型是乙個人可以有多個郵箱,而乙個郵箱只能屬於乙個人,一對多的資料關係,其...