oracle–單表查詢
- 2019 年 10 月 10 日
- 筆記
—單表的查詢學習 –查詢表的所有數據 select * from 表名;*代表所有 select * from emp; –查詢表中指定欄位的值 select 欄位名1,欄位名2,…from表名 select empno from emp; select empno,ename from emp; –給查詢結果中的欄位使用別名 –在欄位名後使用關鍵字 欄位名 as "別名" –作用:方便查看查詢結果 –注意:as關鍵字可以省略不寫,別名中沒有特殊字元雙引號也可以省略不寫。 select empno 員工編號,ename"員工 姓名",job as 工作,mgr as "領導編號" from emp; –連接符:select 欄位名||'字元'||欄位名||….. from 表名 –||為sql語句的字元鏈接符,使用在select和from之間 –字元鏈接格式為 欄位名||'字元'||欄位名 –注意:一個拼接好的連接在結果集中是作為一個新的欄位顯示,可以使用別名優化欄位顯示。 select empno||'的姓名是'||ename as"資訊",job||'哈哈'||mgr from emp; –去除重複 select distinct 欄位名,欄位名,…fromn 表名 —注意:去除重複的規則是按照行進行去除的,多行數據完全相同取其一 select distinct job ,mgr from emp; –排序 –單欄位排序 –select * from 表名 order by 欄位名 asc 升序排序 asc可以省略不寫 –select * from 表名 order by 欄位名 desc 降序序排序 –多欄位排序 –select * from emp order by 欄位名1,欄位名2… –先按照欄位1排序,如果欄位1的值相同,則按照欄位2排序,…. select * from emp order by empno desc–單欄位排序 降序 select empno,ename,job from emp order by ename asc–單欄位排序 升序 select * from emp order by empno,ename–多欄位排序 –欄位的邏輯運算 –select關鍵字和from關鍵字之間的欄位可以直接進行四則運算 –欄位與欄位之間也可以直接進行運算 –注意:欄位值為數值類型 select * from emp select empno,ename,job,sal*2+1000,sal+comm from emp —————————————————————– –使用where子句查詢篩選 –select 欄位名,欄位名,…from表名 where 篩選條件 –單篩選條件 –使用運算符進行篩選 =,>,>=,<,<=,<> 單個條件中 –注意:如果條件中的值為字元,必須使用單引號括起來 –查詢所有的員工的工資資訊 select empno,ename,sal+comm as 薪資 from emp –查詢SMITH的個人資訊 select * from emp where ename='SMITH' –查詢SMITH的薪資資訊,邏輯運算符= select empno,ename,sal,sal+comm from emp where ename='SMITH' –查詢工資大於1000的員工資訊,邏輯符> select * from emp where sal>'2000' –查詢工資不等於3000的員工資訊 select * from emp where sal<>3000 order by sal –練習: –查看工資等於1250的員工資訊 select *from emp where sal='1250' –查看工作等於CLERK的員工資訊 select * from emp where job='CLERK' –查看工資大於1250的員工姓名和工作 select ename,job from emp where sal>1250 –查看工資大於等於2000的員工資訊 select * from emp where sal>=2000; –查看工資小於等於2000的員工資訊; select * from emp where sal<=2000; –查看工資不等於1500的員工資訊 select * from emp where sal<>1500; –查看入職日期在81年後的員工資訊 –注意:oracle默認的日期格式為 日-月-年,示例'03-1月-1981' select * from emp order by hiredate select * from emp where hiredate>='01-1月-1981' order by hiredate –多條件篩選(where子句關鍵字:and,or,like,is null,is not null, in ,between and) –查詢工資在2000-3000之間的員工資訊 –使用and關鍵字,多條件同時成立的篩選使用and關鍵字進行條件連接 select * from emp where sal>=2000 and sal<3000 –使用between and 關鍵字進行條件連接,包含兩頭的數據 select * from emp where sal between 2000 and 3000 –查詢工作為SALESMAN,ANALYST,MANAGER的員工資訊 –使用or關鍵字,進行或條件的篩選。 select * from emp where job='SALESMAN' or job='ANALYST' or job='MANAGER' order by job –使用in關鍵字,也可以進行或篩選,但是in中的內容只能為一個欄位的值。 select * from emp where job in('SALESMAN','ANALYST','MANAGER') –查詢姓名中包含s的,以s開頭的,以s結尾的,第二個字元為A的。(模糊查詢) –%號表任意多個的任意字元 –select * from 表名 where 欄位名 like '%字元%' 查詢包含指定字元的數據 select * from emp where ename like '%S%' –包含s的 –select * from 表名 where 欄位名 like '字元%' 查詢以指定字元開頭的數據 select * from emp where ename like 'S%'–以S開頭 –select * from 表名 where 欄位名 like '%字元' 查詢以指定字元結尾的數據 select * from emp where ename like '%S'–以S結尾的 –select * from 表名 where 欄位名 like '_字元%' 查詢指定位置為指定字元的數據 –_表示一個任意字元 select * from emp where ename like '_A%'–第二個字元為A的 –select * from 表名 where 欄位名 like '%字元2字元1%' escape'字元2' –escape將指定的字元變為轉義字元 –轉義字元可以將特殊字元轉為普通字元 select * from emp where ename like '%/_%' escape '/' select * from emp for update –查詢有津貼的員工資訊 — select * from 表名 where 欄位名 is null 欄位值為null — select * from 表名 where 欄位名 is not null 欄位值不為null –多個條件使用and關鍵進行連接,篩選的是符合所有條件的數據 –select * from 表名 where 篩選條件1 and 條件2 and …. select * from emp where comm is not null and comm>0
使用group by分組 在多行函數中不能直接使用普通欄位,除非group by 在多行函數中不能直接使用單行函數,除非group by group by學習: ---1、使用group by進行數據分組 select 多行函數,分組欄位 from 表名 group by 分組欄位 ---2、多欄位進行分組的時候,按照欄位順序進行分組,第一條件分組完成後,繼續使用其他條件依次分組。 ---3、group by依然可以和order by 聯合使用 ---4、可以和單行函數聯合進行分組,注意使用了單行函數那麼在查詢語句中必須也要使用 查詢最高工資和員工數 select max(sal),count(*) from emp 查詢不同部門的最高工資 select * from emp order by deptno select deptno,max(sal) from emp group by deptno--使用group進行分組查詢,分組的欄位可以出現在查詢中,其他欄位依然不可以 查詢不同工作崗位的員工數 select * from emp for update select lower(job),count(*) from emp group by lower(job)--使用單行函數進行分組 查詢不同部門的不同工作崗位的人數 select deptno,job ,count(*) from emp group by deptno,job--使用多欄位組合進行分組 select deptno,job ,count(*) from emp group by deptno,job order by deptno 查詢不同部門的不同工作崗位的並且人數大於1的資訊 select count(*) from emp where count(*)>3 group by deptno select deptno,job ,count(*) from emp where count(*)>1 group by deptno,job order by deptno 查詢部門號大於10的不同部門的不同工作崗位的人數 select deptno,job ,count(*) from emp where deptno>10 group by deptno,job order by deptno 使用having進行分組後篩選 having學習: --1、使用group by分組後在進行數據篩選的時候,where中不能出現多行函數,所以使用新的關鍵字having進行條件篩選 --2、where條件篩選的執行順序:from-->where--->group -->select --3、having條件篩選的執行順序:from-->group by -->having-->select --4、where的執行效率比having要高,能使用where的情況下盡量不要使用having 查詢不同部門的不同工作崗位的並且人數大於1的資訊 使用where語句進行篩選 where條件語句sql執行順序:from-->where--->group -->select select count(*) from emp where count(*)>1 group by deptno,job 使用having語句進行篩選 having條件語句的執行順序:from-->group by -->having-->select select deptno, count(*) from emp group by deptno having count(*)>5 select deptno,job ,count(*) from emp group by deptno,job having deptno>10 order by deptno