Oracle中的特殊判式

2021-09-24 14:48:02 字數 2897 閱讀 2098

除了邏輯運算之外,oracle提供了一些特殊判式。這些判式可以用來生成更加複雜和靈活的查詢條件。本節將著重介紹以下幾種判式。

between: 取值範圍

in: 集合成員測試

like: 模式匹配

is null: 空值判斷

all,somne,any:數量判斷。

exists:存在性判斷

between——範圍測試

between判式,用於判斷某個值是否在另外兩個值之間。這些值可以為數值型、字串和日期型。使用betwwen判式來獲得id號在1-5之間的員工資訊。

select * from t_employees where employee_id between 1 and 5;

betwwen判式同樣可以應用於字串和日期型。字串是按照字母表的順序進行比較,而日期型是按照日期的先後順序進行比較。

select * from t_employees where 'b' between 'b' and 'c';

select * from t_employees where 'b' between 'bc' and 'c';

注意與說明:between判式與》=、<=的組合是等價關係。但是,效率上要比後者差。

in——集合成員測試

in用於判斷某個值是否乙個集合的成員。

select * from t_employees where status in('new', 'act');

值得注意的是,in判式中的集合的成員的資料型別可以不一致,例如,select * from t_employees where status in('new', 'act', sysdate, 1)中的資料型別包含了字串、日期型和數值型。

like——模式匹配

like判式的最大特點在於,可以使用萬用字元。其通常的應用場景為處理模糊查詢。

select * from t_employees where employee_name like '鐘%';

如果要求字串中含有原義字元"%",例如,含有百分比的字串。那麼,like判式應寫作:like '鐘\%' escape '\'。oracle會首先解釋escape關鍵字,並將其後的字元"\"解釋為轉義字元。那麼在"鐘\%"中的"%"不再表示萬用字元,而是表示原義字元"%"。

"_"(下劃線)是可用於like判式的另乙個萬用字元,該萬用字元表示乙個任意的字元。

is null——空值判斷

在邏輯判斷中,對於列值為空的判斷,不能使用=或者<>。oracle對與空值的判斷提供了專門的判式——is null。例如,為了獲取表t_employees中員工資訊不全的記錄,可以利用如下所示的查詢語句。

select * from t_employees

where employee_id is null

or employee_name is null

or work_years is null

or status is null;

exists——存在性判斷

in判式用於判斷表的列值是否存在於列表(集合)中。而exists判式則可用於判斷查詢結果集合是否為空。例如,為了查詢出表t_employees所儲存的員工資訊中,哪些員工存在於工資表中,即可利用exists判式。

select * from t_employees e

where exists(select * from t_salary where employee_id = e.employee_id);

all,some,any——數量判斷

all,some和any判式的作用物件為記錄集合。all表示,記錄集中的所有記錄,some表示其中的一些記錄,any判式則表示其中的任意記錄。例如,在員工工資表t_salary中,為了查詢高於id為4和5的工資資訊,即可使用all判式。

select * from t_salary where employee_id = 4 or employee_id = 5;

select * from t_salary where salary > all(select distinct salary from t_salary where employee_id = 4 or employee_id = 5);

select * from t_salary where salary > some(select distinct salary from t_salary where employee_id = 4 or employee_id = 5);

此時的some判式實際相當於邏輯運算中的or運算,即salary>6000 or salary>7000。此時,使用any判式,將返回同樣的結果。

some,any,all的用法和區別

select ename,sal

from emp

where sal > any(select sal from emp where deptno = 10);

從emp表中選出大於部門10中最小工資的的人的姓名和工資。

小結:any是大於最小,小於最大。(當然此處使用some也可以,但any語義更為直接)

select ename,sal

from emp

where sal = some(select sal from emp where deptno = 30) and deptno not in (select deptno from emp where deptno = 30);

從emp表中選出不是30號部門,但工資和30號部門任何乙個人的工資相等的人的姓名和工資。

小結:等於任意乙個。

select ename,sal

from emp

where sal > all(select sal from emp where deptno = 20);

從emp表中選出工資大於20號部門最大工資的人的姓名及工資。

小結:大於最大,小於最小。

php empty isset對特殊字元的判斷

php empty函式判斷特殊字元為空測試 a true if empty a else a false if empty a else a null if empty a else a true if empty a else a null if empty a else a 0 if empty...

oracle中 特殊字元 轉義 ( )

在dml中,若操作的字元中有 特殊字元,則會被oracle視作是輸入變數的標誌,此時需要用轉義字元來進行轉義。這個是oracle 裡面用來識別自定義變數的設定,現在我們在sql plus下將其關閉 sql set define off 然後再次執行匯入指令碼,ok!問題搞定。注意 如果是在toad中...

在Oracle中輸入「 」特殊字元

在oracle中,由於有一些特殊字元,所以如果我們輸入字串中遇到這些特殊字元而直接輸入的話,sql語句會出現錯誤,這裡說一些oracle中 這個字元如何輸入。這個字元在oracle中的意思是指定引數,如果直接用在sql語句中,比如說 select from test table where cola...