單表查詢是相對多表查詢而言的,指從一個數據表中查詢數據。 4.2.1 查詢所有的記錄 在【命令編輯區】執行輸入“select * from scott.emp”,然后單擊【執行】按鈕,出現如圖4.3所示的emp數據表所有記錄。 【參見光盤文件】:/第4章/4.2/421.sql。 select * from 數據表,這里的“*”代表數據表中所有的字段。 4.2.2 查詢所有記錄的某些字段 在【命令編輯區】輸入“select empno,ename,job from scott.emp”,然后單擊【執行】按鈕,將顯示emp數據表的empno、ename和job字段,如圖4.4所示。 【參見光盤文件】:/第4章/4.2/422.sql。 select 字段名1, 字段名2,…… from 數據表,將顯示某些特定的字段,注重這里的字段名之間的逗號是英文狀態下的逗號。 4.2.3 查詢某些字段不同記錄 在圖4.4所示的job字段中,可以發現有相同的數據,為了查詢有多少種不同的job,在【命令編輯區】輸入“select distinct job from scott.emp”,然后單擊【執行】按鈕,出現如圖4.5所示的結果。 【參見光盤文件】:/第4章/4.2/423.sql。 select distinct 字段名 from 數據表,這里的“distinct”保留字指在顯示時去除相同的記錄,與之對應的是“all”將保留相同的記錄,默認為“all”。 4.2.4 單條件的查詢 (1)在【命令編輯區】輸入“select empno,ename,job from scott.emp where job=’MANAGER’”,然后單擊【執行】按鈕,出現如圖4.6所示的字符型字段條件查詢的結果,查詢的是job為MANAGER的記錄。 【參見光盤文件】:/第4章/4.2/424-1.sql。 (2)在【命令編輯區】輸入“select empno,ename,sal from scott.emp where sal<=2500”,然后單擊【執行】按鈕,出現如圖4.7所示的數字型字段條件查詢的結果,查詢的是滿足sal小于等于2500的記錄。 【參見光盤文件】:/第4章/4.2/424-2.sql。 where可以指定查詢條件,假如是指定字符型字段查詢條件,形式為字段名 運算符 '字符串';假如是指定數字型字段查詢條件,形式為字段名 運算符 '字符串'。 單條件查詢使用的比較運算符如表4.1所示。 【參見光盤文件】:/第4章/4.2/table41.sql。 表4.1 比較運算符名稱實例=(等于)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所示的結果。