Oracle-單表查詢
最簡單查詢方式
查詢表中所有記錄的所有欄位資料:
select */1 from 表名;
select * from dep;
查詢制定欄位
只查詢emp表中員工的 ID,姓名,工資,獎金這四個欄位
select empid,ename,esalary,comm from emp;
查詢顯示別名
select 欄位 as 別名 from 表名 select name as "姓名" , sex 性別 , id as ID , age as "年齡" from emp select name as "姓名" , sex 性別 , id as ID , age as "年齡" , msalary*12+4400 年收入 from emp
注意:別名中 as 可有可無,"“雙引號也可以可有可無,但是當別名中有空格必須加”"
條件查詢
獲取部分記錄
select 欄位,... from 表名 where 條件
算數操作符
+(加),- (減),* (乘),/ ( / 是除號,結果是浮點數) ,mod(m,n)(m 對 n 求餘)
select sal, sal*1.1, sal+1000, sal-1000, sal / 1000, mod(sla,1000) from emp;
比較操作符
=(等於),>(大於)< ,(小於) , <=(大於等於), >=(小於等於), !=(不等於), <> (不等於) select * from student where score <= 90 select * from student where score <> 90
注意:!=和<>都是不等於的意思
邏輯操作符
and (與) ,or(或) ,not(非)
between ... and ...(檢查是否在兩個值之間並且包括兩個值),[ not ] in(與列表中的值匹配),[ not ] is null(檢查是否為空)
select * from student where score between 70 and 90
相當於
select * from student where score <= 90 and score >= 70
連線操作符
|| 用於將兩個或者多個字串合併成一個字串,或者將一個字串與一個數值合併在一起 select ('我叫' || name || '年齡是' || age) as "自我介紹" from student
注意:括號內用單引號,括號後的 as 可以不加,"" 雙引號中沒有空格可以不加 “”,如果有空格要加 “”,比如 “自我介紹” 可以不加 ,但是"自我 介紹"必須加引號,()可以不加,但是最好加上,方便閱讀
去除重複行查詢
distinct
select distinct job from emp;
select distinct job,deptno from emp;
模糊查詢
like( _任意一個字元 ,%任意多個字元,ESCAPE '?' 可以把雙引號中任意字元變成轉義識別符號 )
--查詢姓王的員工
select * from emp where ename like '王%' ----模糊匹配使用like %任意多個字元
--查詢名字中有王字的員工
select * from emp where ename like '%王%' ----只要包含王即可,位置無要求
--查詢第二個字是小字的員工
select * from emp where ename like '_小%' ----只要第二是王 _任意一個字元
--出現第二個字是_(下劃線)的員工
select * from emp where ename like '%\_%' ESCAPE '\' ----ESCAPE '?' 可以把雙引號中任意字元變成轉義識別符號
排序
order by 排序參考欄位 desc/asc
--按照工資升序排列
select * from emp order by salary;(預設升序)
select * from emp order by salary asc;
--按照工資降序排列
select * from emp order by salary desc;
聚合函式/分組函式
sum()求和,avg()平均值,max()最大值,min()最小值,count(1)/count(*)總數
--獲取所有員工的平均工資avg(),最高工資max(),最低工資min(),工資總額sum(),人數count()
select avg(sal),max(sal),min(sal),sum(sal),count(sal) from emp;
分組函式是對錶中一組記錄進行操作,每組值返回一個結果,即首先要對錶記錄進行分組,然後再進對錶記錄進行分組,然後在進行操作彙總,每組返回一個結果,分組是可能是整個表分為一個組,也可能根據條件分成多組。
-
count(1):如果不分組,得到是表的記錄數量;如果有分組,在分組後,得到的是,每一個分組的數量
-
sum(score):如果不分組,得到是表的總分數;如果有分組,在分組後,得到的是,每一個分組的總分
-
max(score):如果不分組,得到是表中分數最大值;如果有分組,在分組後,得到的是,每一個分組中的最大分數
-
min(score):如果不分組,得到是表中分數最小值;如果有分組,在分組後,得到的是,每一個分組中的最小分數
-
avg(score):如果不分組,得到是表中分數平均值;如果有分組,在分組後,得到的是,每一個分組的平均分
分組查詢
group by 分組依據欄位 (用於將表劃分為組,對查詢結果按組進行聚合運算,為每組返回一個結果,把by後面的資料進行分組,如果後面是一個欄位,就把一個欄位當成一組,如果是兩個欄位就把兩個欄位當成一組,以此類推)
having 篩選條件(用來指定 group by 的檢索條件,通常與 group by語句聯合使用,用來過濾由 group by 語句返回的記錄集,也就是說用來篩選分組後的資訊)
--查詢學生表中男生女生各自的數量(根據性別分組)
select ssex,count(ssex) from students group by ssex;
--查詢平均分高於80分的學生記錄(根據學號分組,再根據分組後平均成績輸出結果)
select sid, count(course), avg(score) from student group by sid having avg(score)>=80;
注意:帶有 group by 子句的查詢語句中,在 select 列表中指定的欄位要麼是 group by 子句中指定的欄位,要麼包含聚組函式
where 和 having 區別
where 是分組之前的篩選,having 是分組之後的篩選,注意語句執行順序
having 中可以出現分組函式/聚合函式,where 中不可以,因為和執行順序有關,注意語句執行順序
篩選條件可以放入where,也可以放入having ,建議選擇where
語句的執行順序
from > where > group by > having > select > order by
!!!!!!!!order by 永遠是最後執行
語句的書寫順序
select > from > where > group by > having > order by
函式
字元函式
----獲取ascii值------ascii( )
select ascii('a') from dual --97
select ascii('A') from dual --65
----連線字串------concat( )
select concat(ename,ejob) from emp
select concat('Hello','World') from dual
----查詢字串------instr(從哪裡查詢,需要查詢的字元)
select instr('Hello World','orld') from dual
----字串長度------length( )
select length(ejob),ejob from emp
----大寫輸出------upper( )
select upper(ejob),ejob from emp
----小寫輸出------lower( )
select lower(ejob),ejob from emp
----去除左邊匹配的字元------ltrim( ' ',' ' )
select ltrim('---lucy---','-')from dual
----去除右邊匹配的字元------rtrim( ' ' ,' ' )
select rtrim('---lucy---','-')from dual
----去除匹配的字元------trim(' ' from ' ')
select trim('-'from '---lucy---')from dual
----替換字元------replace(' ',' ',' ')
select replace('Hello World','World','Oracle') from dual
----擷取字串------substr('', , )--------H是1
select substr('Hello Oracle',1,7)from dual
select substr('Hello Oracle',3)from dual
數字函式
----取絕對值------abs()
select abs(-1) from dual;
----餘弦------cos()
select cos(0.6) from dual
----反餘弦------acos()
select acos(1.05) from dual
----正弦------sin()
select sin(0.6) from dual
----向上取整------ceil()
select ceil(1.2) from dual;
----向下取整------floor()
select floor(1.2) from dual;
----對數------log()
select log(10,100) from dual
----四捨五入------round()
select round(1.4) from dual;
select round(1.4999999) from dual;
select round(1.5) from dual;
----取餘------mod()
select mod(30,10) from dual
----冪次------power()
select power(2,2) from dual
----平方根------sqrt()
select sqrt(4) from dual
----保留幾位小數------trunc()
select trunc(1.1234566,3) from dual
日期函式
-----查詢系統時間,系統時間戳
select sysdate,systimestamp from dual
----加月份-----add_months(時間,新增月) !!!!!!!
select sysdate,add_months(sysdate,3) from dual
----某個月最後一天------last_day() !!!!!!
select last_day(sysdate) from dual
----四捨五入------round()
select round(sysdate,'year') from daul
select round(add_months(sysdate,-3),'year') from dual
----提取日期------extract()
--------提取年月日用 sysdate ,提取時分秒用 systimestamp
select sysdate ,
extract(year from sysdate) year,
extract(month from sysdate) month,
extract(day from sysdate) day,
extract (hour from systimestamp) hour, ----- 獲取的是原時區的時間
extract (minute from systimestamp) minute,
extract (second from systimestamp) second
from dual;
轉換函式
----轉化為字元型別------to_char()
select to_char(sysdate,'yyyy-mm-dd') from dual
select to_char(sysdate,'yyyy"年"mm"月"dd"日" HH24:MI:SS') from dual ----單引號中雙引號
select 45678.1234,to_char(45678.1234,'$999,999.999999') from dual
select 45678.1234,to_char(45678.1234,'L000,000.000000') from dual
----轉化為日期型別------to_date()
select to_date('2018-10-02 11:11:11','yyyy"-"mm"-"dd HH24:MI:SS') from dual--
----轉化為數值型別------to_number()
select to_number('$123','$2342') from dual
----處理null資料
------nvl(m,value) 若m為空返回value值,若不為空返回m值
------nvl2(m,value1,value2) 若m非空返回value1,否自返回value2
--查詢員工年收入,如果沒有獎金則獎金為1000,有獎金獎金就為該數值
select eid,ename,esalary*12+nvl(ecomn,1000) from emp
--查詢員工年收入,如果沒有獎金獎金為2000,有獎金獎金加500
select eid,ename,esalary*12+nvl2(ecomn,ecomn+500,2000) from emp
-- nvl(comm,1000) == if (comm is null) then comm = 1000 if單分支
-- nvl2(comm,ecomn+500,2000) == if (comm is not null) then ecomn+500 else 2000 if雙分支