WHERE(2)——使用AND,OR,IN,NOT
使用AND
--檢索由供應商1003制造的,價格在在10以下的所有產品的名稱和價格 select prod_id,prod_price,prod_name from products where vend_id = 1003 and prod_price <= 10;
使用OR
--檢索由兩個供應商之一制造的任何產品和價格 select prod_name,prod_price from products where vend_id = 1002 or vend_id = 1003;
求值順序:AND > OR
--檢索供應商為1002的所有產品,供應商為1003且價格超過10的產品 select prod_name,prod_price from products where vend_id = 1002 or vend_id = 1003 and prod_price >=10; --檢索供應商為1002或1003,且產品價格超過10的產品 select prod_name,prod_price from products where (vend_id = 1002 or vend_id = 1003) and prod_price >=10;
使用IN
select prod_name,prod_price from products where vend_id in (1002,1003)/*括號內:有效值列表*/ order by prod_name;
IN優於OR:
1.求值順序更容易管理(因為它使用了比OR更少的運算符)。
2.執行更快。
3.IN運算符可以包含一條新的select語句。
使用NOT
select prod_name,prod_price from products where vend_id not in (1002,1003) order by prod_name;
通配符
通配符(Wildcard):用於匹配值的某些部分的特殊字符。
搜索模式(Search Pattern):由字面意義的文本、通配符字符或者二者的任意組合構成的搜索條件。
%:“匹配出現任意次數的任意字符——可以是0次”
select prod_id,prod_name from products where prod_name like 'Jet%'; select prod_id,prod_name from products where prod_name like '%anvil%';
注意:①大小寫敏感;②留心尾部空格(解決:搜索模式末尾加“%”;用函數剪切空格);③留心null(“%”不匹配null值)。
_:“只匹配單個字符——不多也不少”
select prod_name,prod_price from products where prod_name like '_ ton anvil%';
注意:①不要過度的使用通配符;②盡量不要在開頭使用通配符(速度變慢);③注意通配符的位置。
正則表達式——簡易(正則表達式后續單獨寫一篇~~)
正則表達式是什么?
正則表達式是一種用於匹配文本的特殊語言的一部分。例如:需要從文本中提取電話號碼,可能會需要使用一個正則表達式。
Oracle PL/SQL 提供了四種可以訪問正則表達式的函數:
regexp_like( ):用於搜索字符。
regexp_replace( ):用於替換字符串中的字符。
regexp_instr( )、regexp_substr( ):在字符串內搜索字串。
示例:
--基本的字符匹配 select prod_name from products where regexp_like(prod_name, '1000') order by prod_name; /* 對比: 1. where子句需要給它傳遞一個列名稱,一個值,以及一個運算符(例如=,like)、 regexp_like()是一個接受參數的函數,它會返回TRUE和FALSE,TRUE的話就匹配where子句,並且返回它。 2. like匹配整個列,如果要匹配的文本位於列值的中間,like將不會找到它。 regexp_like()會在列值中尋找匹配,若要匹配的部分位於列值中間,會找到它並返回行。 */ --“.”:“匹配任意單個字符” select prod_name from products where regexp_like(prod_name, '.000') order by prod_name; --“|”:or匹配 select prod_name from products where regexp_like(prod_name, '1000|2000|3000') order by prod_name; 例子: select prod_name from products where regexp_like(prod_name, '1|2|3 ton')/*將匹配1或2或3 ton*/ --匹配多個字符之一“[]” select prod_name from products where regexp_like(prod_name, '[123] ton')/*將匹配1 ton或2 ton或3 ton*/ order by prod_name; --“^”:字符集取反[^123] select prod_name from products where regexp_like(prod_name, '[^123] ton') order by prod_name; --匹配范圍[0-9];[a-z] select prod_name from products where regexp_like(prod_name, '[1-5] ton') order by prod_name; --匹配特殊字符:需要在前面加“\” select vend_name from vendors where regexp_like(vend_name, '\.') order by vend_name; /*這個過程稱為轉義(Escape)*/
匹配字符類別
類別 | 描述 |
\d | 任意數字(等同於[0-9]) |
\D | 任意非數字字符(等同於[^0-9]) |
\w | 任意字母或數字(等同於[a-zA-Z0-9]) |
\W | 任意非字母或數字(等同於[^a-zA-Z0-9]) |
\s | 任意空白字符 |
\S | 任意非空白字符 |
匹配重復次數
元字符 | 描述 |
* | 0個或多個匹配 |
+ | 1個或多個匹配(等價於{1,}) |
? | 0個或1個匹配(等價於{0,1}) |
{n} | 具體的匹配次數 |
{n,} | 不少於指定的匹配次數 |
{n,m} | 匹配的范圍 |
示例:
select prod_name from products where regexp_like(prod_name, '\(\d sticks?\)') order by prod_name; /* 結果: prod_name TNT (1 stick) TNT (5 sticks)
分析: \(:匹配( \d:匹配任意數字 sticks?:匹配stick或sticks */ select prod_name from products where regexp_like(prod_name, '\d{4}')/*匹配任意四個連續的數字*/ order by prod_name; /*還可寫為:'[0-9]{4}'*/
錨
錨用於匹配特定位置的文本(例如查找以某個數字開頭的產品)
錨元字符 | 描述 |
^ | 文本的開頭 |
$ | 文本的末尾 |
select prod_name from products where regexp_like(prod_name, '^[0-9\.]') order by prod_name;
注:“^”在字符集[]內使用時,表示取反;否則表示指示字符串的開頭。使用錨可以使得regexp()像like那樣,只需在開頭加^,在末尾加$。
參考文獻:《Oracle PL\SQL 必知必會》[美] Ben Forta 著 傅強 譯(第7-9章,64-99頁)。