【正文】
多表返回所有行,但除去任何重復(fù)的行。 例:Sql代碼 1. select,fromempe1unionselect, 2. fromempe2select , from emp e1 union select , from emp e2 原則 : (1)被選擇的列數(shù)和列的數(shù)據(jù)類型必須是與所有用在查詢中的SELECT語句一致。列的名字不必相同。 (2)聯(lián)合運(yùn)算在所有被選擇的列上進(jìn)行。 (3)在做重復(fù)檢查的時(shí)候不忽略空(NULL)值。 (4)IN運(yùn)算有比UNION運(yùn)算高的優(yōu)先級。 (5)在默認(rèn)情況下,輸出以SELECT子句的第一列的升序排序。 在例子中將輸出empno,ename,mgr三列數(shù)據(jù)。全聯(lián)合(UNION ALL)運(yùn)算 用全聯(lián)合運(yùn)算從多個(gè)查詢中返回所有行。 原則: (1)和聯(lián)合不同,重復(fù)的行不被過濾,并且默認(rèn)情況下輸出不排序。 不能使用DISTINCT關(guān)鍵字。 (2) 使用:Select statement union | union all Select statement。相交運(yùn)算 用相交運(yùn)算返回多個(gè)查詢中所有的公共行。 無重復(fù)行。原則: (1).在查詢中被 SELECT 語句選擇的列數(shù)和數(shù)據(jù)類型必須與在查詢中所使用的所有的 SELTCT 語句中的一樣,但列的名字不必一樣。 相交的表的倒序排序不改變結(jié)果。 (2). 相交不忽略空值。 (3). 使用:Select statement intersect all Select statement。. minus差集操作相減運(yùn)算 用相減運(yùn)算返回由第一個(gè)查詢返回的行,那些行不出現(xiàn)在第二個(gè)查詢中 (第一個(gè)SELECT語句減第二個(gè)SELECT語句)。 原則: (1)在查詢中被SELECT語句選擇的列數(shù)和數(shù)據(jù)類型必須與在查詢中所使用的所有的SELTCT語句中的一樣,但列的名字不必一樣。 (2)對于MINUS運(yùn)算,在WHERE子句中所有的列都必須在SELECT子句中。 集合運(yùn)算的原則?在兩個(gè)SELECT列表中的表達(dá)式必須在數(shù)目上和數(shù)據(jù)類型上相匹配?可以用圓括號改變執(zhí)行的順序?ORDER BY子句:–只能出現(xiàn)在語句的最后–從第一個(gè)SELECT語句接收列名、別名,或者位置記號注:?除了UNION ALL,重復(fù)行自動被清除?在結(jié)果中的列名是第一個(gè)查詢中出現(xiàn)的列名?除了UNION ALL,默認(rèn)情況下按升序順序輸出二、exists和not exists的使用1. 謂詞exists和in概述Exists用于只能用于子查詢,可以替代in,若匹配到結(jié)果,則退出內(nèi)部查詢,并將條件標(biāo)志為true,傳回全部結(jié)果資料.in不管匹配到匹配不到都全部匹配完畢.使用exists可以將子查詢結(jié)果定為常量,不影響查詢效果,而且效率高。如查詢所有銷售部門員工的姓名,對比如下:IN is often better if the results of the subquery are very smallWhen you write a query using the IN clause, you39。re telling the rulebased optimizer that you want the inner query to drive the outer query.When you write EXISTS in a where clause, you39。re telling the optimizer that you want the outer query to be run first, using each value to fetch a value from the inner query.In many cases, EXISTS is better because it requires you to specify a join condition, which can invoke an INDEX scan. However, IN is often better if the results of the subquery are very small. You usually want to run the query that returns the smaller set of results first.:,優(yōu)先使用in。,優(yōu)先使用exists。因?yàn)槿粲胕n,則oracle會優(yōu)先查詢子查詢,然后匹配外層查詢,若使用exists,則oracle會優(yōu)先查詢外層表,然后再與內(nèi)層表匹配。最優(yōu)化匹配原則,拿最小記錄匹配大記錄。使用inselect last_name, title from s_emp where dept_id in (select id from s_dept where name=39。Sales39。)。 使用existsselect last_name,title from s_emp e whereexists (select 39。x39。 把查詢結(jié)果定為constant,提高效率 from s_dept s where = and =39。Sales39。)。 not exists的使用 與exists 含義相反,也在子查詢中使用,用于替代not in。其他一樣。如查詢不在銷售部的員工姓名select last_name,title from s_emp e wherenot exists (select 39。x39。 把查詢結(jié)果定為constant,提高效率 from s_dept s where = and =39。Sales39。)。3.with子句Oracle9i新增語法,通過select調(diào)用,一般在with查詢用到多次情況下。,只做一次查詢,提高效率。,第1個(gè)用with,后面的不用with,并且用逗號隔開。,只通過右括號分割,查詢必須用括號括起來,而在查詢中不使用,那么會報(bào)ora32035錯(cuò)誤:未引用在with子句中定義的查詢名。(至少一個(gè)with查詢的name未被引用,解決方法是移除未被引用的with查詢)。With子句目的是為了重用查詢。語法:With alias_name as (select1), as和select中的括號都不能省略alias_name2 as (select2),后面的沒有with,逗號分割…alias_namen as (select n) –與下面的查詢之間沒有逗號Select ….如查詢銷售部門員工的姓名:with clausewith a as(select id from s_dept where name=39。Sales39。 order by id)select last_name,title from s_emp where dept_id in