黑馬oracle_day01:03.oracle的查詢

 01.oracle體系結構html

 02.oracle的基本操做mysql

 03.oracle的查詢sql

 04.oracle對象編程

 05.oracle編程oracle

黑馬oracle_day01:03.oracle的查詢


 

09scott用戶介紹

--- scott用戶,密碼tiger。
-- 解鎖scott用戶
alter user scott account unlock;
-- 解鎖scott用戶的密碼[重置密碼]
alter user scott identified by tiger;
-- 切換登陸到scott用戶下 

 

10單行函數

-- 單行函數:做用於一行,返回一個值。ide

 

---字符函數
select upper('yes') from dual;--結果YES
select lower('YES') from dual;--結果yes

 

----數值函數
select round(26.16, 1) from dual;---四捨五入,後面的參數表示保留的位數
select round(56.16, -2) from dual;---四捨五入,後面的參數表示保留的位數
select trunc(56.16, -1) from dual;---直接截取,不在看後面位數的數字是否大於5
select mod(10, 3) from dual;---求餘數

 

----日期函數
----查詢出emp表中全部員工入職距離如今幾天。
select sysdate-e.hiredate from emp e;
----算出明天此刻
select sysdate+1 from dual;
----查詢出emp表中全部員工入職距離如今幾月。
select months_between(sysdate,e.hiredate) from emp e;
----查詢出emp表中全部員工入職距離如今幾年。
select months_between(sysdate,e.hiredate)/12 from emp e;
----查詢出emp表中全部員工入職距離如今幾周。
select round((sysdate-e.hiredate)/7) from emp e;

 

----轉換函數
---日期轉字符串
select to_char(sysdate, 'yyyy-mm-dd hh:mi:ss') from dual;-- 2019-09-17 10:43:40
select to_char(sysdate, 'fm yyyy-mm-dd hh24:mi:ss') from dual;-- 2019-9-17 10:47:20
---字符串轉日期
select to_date('2018-6-7 16:39:50', 'fm yyyy-mm-dd hh24:mi:ss') from dual;-- 2018/6/7 16:39:50

 

----通用函數
---算出emp表中全部員工的年薪
----獎金裏面有null值,若是null值和任意數字作算術運算,結果都是null。
select e.sal*12+nvl(e.comm, 0) from emp e;-- 若是是null就用0代替去計算

 

11條件表達式

 

---條件表達式
---條件表達式的通用寫法,mysql和oracle通用
---給emp表中員工起中文名
select e.ename, 
       case e.ename
         when 'SMITH' then '曹賊'
           when 'ALLEN' then '大耳賊'
             when 'WARD' then '諸葛小兒'
               --else '無名'
                 end
from emp e;

 

---判斷emp表中員工工資,若是高於3000顯示高收入,若是高於1500低於3000顯示中等收入,
-----其他顯示低收入
select e.sal, 
       case 
         when e.sal>3000 then '高收入'
           when e.sal>1500 then '中等收入'
               else '低收入'
                 end
from emp e;

 

----oracle中起別名,用雙引號。
----oracle專用條件表達式

 

 

12多行函數

--多行函數【聚合函數】:做用於多行,返回一個值。
select count(1) from emp;---查詢總數量
select sum(sal) from emp;---工資總和
select max(sal) from emp;---最大工資
select min(sal) from emp;---最低工資
select avg(sal) from emp;---平均工資

 

 13分組查詢

---分組查詢
---查詢出每一個部門的平均工資
---分組查詢中,出如今group by後面的原始列,才能出如今select後面
---沒有出如今group by後面的列,想在select後面,必須加上聚合函數。
---聚合函數有一個特性,能夠把多行記錄變成一個值。
select e.deptno, avg(e.sal)
from emp e
group by e.deptno;

 

---查詢出平均工資高於2000的部門信息
select e.deptno, avg(e.sal) asal
from emp e
group by e.deptno
having avg(e.sal)>2000;
---全部條件都不能使用別名來判斷。

 

---查詢出每一個部門工資高於800的員工的平均工資
select e.deptno, avg(e.sal) asal
from emp e
where e.sal>800
group by e.deptno;
----where是過濾分組前的數據,having是過濾分組後的數據。
---表現形式:where必須在group by以前,having是在group by以後。

---查詢出每一個部門工資高於800的員工的平均工資
---而後再查詢出平均工資高於2000的部門
select e.deptno, avg(e.sal) asal
from emp e
where e.sal>800
group by e.deptno
having avg(e.sal)>2000;

 

 14多表查詢中的一些概念

 

---多表查詢中的一些概念
---笛卡爾積
select *
from emp e, dept d;
---等值鏈接 select * from emp e, dept d where e.deptno=d.deptno; ---內鏈接 select * from emp e inner join dept d on e.deptno = d.deptno; 

 

---查詢出全部部門,以及部門下的員工信息。【外鏈接】
select *
from emp e right join dept d
on e.deptno=d.deptno;
---查詢全部員工信息,以及員工所屬部門
select *
from emp e left join dept d
on e.deptno=d.deptno;

 

15自鏈接概念和練習

---查詢出員工姓名,員工領導姓名
---自鏈接:自鏈接其實就是站在不一樣的角度把一張表當作多張表。
select e1.ename, e2.ename
from emp e1, emp e2
where e1.mgr = e2.empno;

 

------查詢出員工姓名,員工部門名稱,員工領導姓名,員工領導部門名稱
select e1.ename, d1.dname, e2.ename, d2.dname
from emp e1, emp e2, dept d1, dept d2
where e1.mgr = e2.empno
and e1.deptno=d1.deptno
and e2.deptno=d2.deptno;

 

16子查詢

---子查詢返回一個值
---查詢出工資和SCOTT同樣的員工信息
select * from emp where sal in
(select sal from emp where ename = 'SCOTT')

 

---子查詢返回一個集合
---查詢出工資和10號部門任意員工同樣的員工信息
select * from emp where sal in
(select sal from emp where deptno = 10);

 

---子查詢返回一張表
---查詢出每一個部門最低工資,和最低工資員工姓名,和該員工所在部門名稱
---1,先查詢出每一個部門最低工資
select deptno, min(sal) msal
from emp 
group by deptno;
---2,三表聯查,獲得最終結果。
select t.deptno, t.msal, e.ename, d.dname
from (select deptno, min(sal) msal
      from emp 
      group by deptno) t, emp e, dept d
where t.deptno = e.deptno
and t.msal = e.sal
and e.deptno = d.deptno;

 

 

17分頁查詢

----oracle中的分頁
---rownum行號:當咱們作select操做的時候,
--每查詢出一行記錄,就會在該行上加上一個行號,
--行號從1開始,依次遞增,不能跳着走。

----排序操做會影響rownum的順序
select rownum, e.* from emp e order by e.sal desc
----若是涉及到排序,可是還要使用rownum的話,咱們能夠再次嵌套查詢。
select rownum, t.* from(
select rownum, e.* from emp e order by e.sal desc) t;


----emp表工資倒敘排列後,每頁五條記錄,查詢第二頁。
----rownum行號不能寫上大於一個正數。
select * from(
    select rownum rn, tt.* from(
          select * from emp order by sal desc
    ) tt where rownum<11
) where rn>5

 

====================函數

endspa

相關文章
相關標籤/搜索