




| 名稱 | 實例 |
| =(等于) | select * from scott.emp where job=’manager’; |
| select * from scott.emp where sal=1100; | |
| != (不等于) | select * from scott.emp where job!=’manager’; |
| select * from scott.emp where sal!=1100; | |
| ^=(不等于) | select * from scott.emp where job^=’manager’; |
| select * from scott.emp where sal^=1100; | |
| <>(不等于) | select * from scott.emp where job<>’manager’; |
| select * from scott.emp where sal<>1100; | |
| <(小于) | select * from scott.emp where sal<2000; |
| select * from scott.emp where job<’manager’; | |
| >(大于) | select * from scott.emp where sal>2000; |
| select * from scott.emp where job>’manager’; | |
| <=(小于等于) | select * from scott.emp where sal<=2000; |
| select * from scott.emp where job<=’manager’; | |
| >=(大于等于) | select * from scott.emp where sal>=2000; |
| select * from scott.emp where job>=’manager’; | |
| in(列表) | select * from scott.emp where sal in (2000,1000,3000); |
| select * from scott.emp where job in (’manager’,’clerk’); | |
| not in(不在列表) | select * from scott.emp where sal not in (2000,1000,3000); |
| select * from scott.emp where job not in (’manager’,’clerk’); | |
| between(介于之間) | select * from scott.emp where sal between 2000 and 3000; |
| select * from scott.emp where job between ’manager’ and ’clerk’; | |
| not between (不介于之間) | select * from scott.emp where sal not between 2000 and 3000; |
| select * from scott.emp where job not between ’manager’ and ’clerk’; | |
| like(模式匹配) | select * from scott.emp where job like ’m%’; |
| select * from scott.emp where job like ’m__’; | |
| not like (模式不匹配) | select * from scott.emp where job not like ’m%’; |
| select * from scott.emp where job not like ’m__’; | |
| is null (是否為空) | select * from scott.emp where sal is null; |
| select * from scott.emp where job is null; | |
| is not null(是否為空) | select * from scott.emp where sal is not null; |
| select * from scott.emp where job is not null; |
like和not like適合字符型字段的查詢,%代表任意長度的字符串,_下劃線代表一個任意的字符。like ‘m%’ 代表m開頭的任意長度的字符串,like ‘m__’ 代表m開頭的長度為3的字符串。
4.2.5 組合條件的查詢
(1)在【命令編輯區】輸入“select empno,ename,job from scott.emp where job>=’clerk’ and sal<=2000”,然后單擊【執行】按鈕,出現如圖4.8所示的邏輯與組合查詢的結果。
【參見光盤文件】:/第4章/4.2/425-1.sql。
(2)在【命令編輯區】輸入“select empno,ename,job from scott.emp where job>=’clerk’ or sal<=2000”,然后單擊【執行】按鈕,出現如圖4.9所示的邏輯或組合查詢的結果。
【參見光盤文件】:/第4章/4.2/425-2.sql。
(3)在【命令編輯區】輸入“select empno,ename,job from scott.emp where not job=’clerk’”,然后單擊【執行】按鈕,出現如圖4.10所示的邏輯非組合查詢的結果。
【參見光盤文件】:/第4章/4.2/425-3.sql。
“not job=’clerk’”等價于“job<>’clerk’”。
組合條件中使用的邏輯比較符如表4.2所示。
【參見光盤文件】:/第4章/4.2/table42.sql。
表4.2 邏輯比較符
| 名稱 | 實例 |
| and(與) | select * from scott.emp where job=’manager’ and sal<>2000; |
| or (或) | select * from scott.emp where job!=’manager’ or sal<>2000; |
| not(非) | select * from scott.emp where not job>=’manager’; |
4.2.6 排序查詢
在【命令編輯區】輸入“select empno,ename,job from scott.emp where job<=’clerk’ order by job asc,sal desc”,然后單擊【執行】按鈕,出現如圖4.11所示的排序查詢的結果。
【參見光盤文件】:/第4章/4.2/426.sql。
order by 可以指定查詢結果如何排序,形式為字段名 排序關鍵詞;asc代表升序排列,desc代表降序排列,多個排序字段之間通過逗號分割。若有where查詢條件,order by要放在where語句后面。
4.2.7 分組查詢
分組查詢是指將查詢結果按照字段分組。
(1)在【命令編輯區】輸入“select empno,ename,job,sal from scott.emp group by job,empno,ename,sal having sal<=2000”,然后單擊【執行】按鈕,出現如圖4.12所示的分組查詢的結果。
【參見光盤文件】:/第4章/4.2/427-1.sql。
(2)在【命令編輯區】輸入“select empno,ename,job,sal from scott.emp where sal<=2000 group by job,empno,ename,sal”,然后單擊【執行】按鈕,出現如圖4.13所示的分組查詢的結果。
【參見光盤文件】:/第4章/4.2/427-2.sql。
where檢查每條記錄是否符合條件,having是檢查分組后的各組是否滿足條件。having語句只能配合group by語句使用,沒有group by時不能使用having,但可以使用where。
4.2.8 字段運算查詢
可以利用幾種基本的算術運算符來查詢數據。
常見的+(加)、-(減)、*(乘)、/(除)4種算術運算都可以用來查詢數據。
在【命令編輯區】輸入“select empno,ename,sal,mgr,sal+mgr from scott.emp”,然后單擊【執行】按鈕,出現如圖4.14所示的結果。
【參見光盤文件】:/第4章/4.2/428.sql。
利用算術運算符僅僅適合多個數值型字段或字段與數字之間的運算。
4.2.9 變換查詢顯示
在【命令編輯區】輸入“select empno 編號,ename 姓名,job 工作,sal 薪水 from scott.emp”,然后單擊【執行】按鈕,出現如圖4.15所示的結果,可以將默認的字段名以設定的名稱顯示。
【參見光盤文件】:/第4章/4.2/429.sql。
以上我們學習了對單個數據表的查詢語句。將上面這些基本的實例經過組合,就可以完成基本的日常數據查詢任務,接下來進一步學習多表查詢。
新聞熱點
疑難解答