標籤:
進階子查詢 子查詢(subquery)
知識點一: 多表串連之間的關係
例如 希望知道哪種種類的書籍已經借出去了。
select distinct c.parent_catagory, c.sub_category
from category c, bookshelf b, bookshelf_checkout bc
where c.category_name = b.category_name and b.title = bc.title
distinct 關鍵字 去重
此例中的3表關聯的 之間的關係:要串連3表必須把其中兩個表與第三個表串連起來。
1.CATEGORY 表串連到 BOOKSHELF表, 它們串連的結果再與 BOOKSHELF_CHECKOUT表串連。 DISTINCT字句去重
注意:並不是每個表都與其它每個表相串連。表之間的串連通常比被串連的表的數量少1.
知識點二: 相互關聯的子查詢
a.where 子查詢 where字句 可以包含SELECT語句的子查詢, where 子查詢的子句where 還可以嵌套
select distinct c.parent_categorty, c.sub_category
from categort c
where category_name in
(select category_name from bookshelf
where title in
(select title from bookshelf_checkout)
);
這個查詢與知識點一中的查詢結果一樣
a: 子查詢可以引用在其主查詢中使用的表的列(即下例中 bookshelf 表中沒有title列)
select title from bookshelf_author
where title in
(select title from bookshelf
where author_name = ‘setphena hay gould’)
Oracle 第13章 當一個查詢依賴另一個查詢