Oracle學(xué)習(xí)查詢操作的筆記以及語句

字號:


    最近仔細(xì)學(xué)習(xí)了下Oracle,為大家準(zhǔn)備了一份學(xué)習(xí)筆記,希望對大家的學(xué)習(xí)有所幫助
    一、Oracle學(xué)習(xí)查詢語句的筆記
    在Oracle 中是對大小寫 敏感的 也就是區(qū)分大小寫
    || 連接or 或者
    and 和、 并且
    where 條件
    between ..最小值 and 最大值(這個關(guān)鍵字包含等于)
    in 字段 in(值1,值2,值3...值n)
    or 關(guān)鍵字 例子----select * from emp where (deptno=10 and job ='MANAGER') or (deptno=20 and job='CLERK');
    distinct去掉重復(fù)的
    like |模糊查詢(注意使用通配符的問題 ) 可以在任何地方應(yīng)用
    |主要的通配符有
    |···|“%”:可以匹配任意長度的內(nèi)容
    |···|“_”:可以匹配一個長度的內(nèi)同
    例如
    select * from emp where ename like '%M%';
    <> 不等號
    #######基本查詢語句結(jié)束#######接下來是排序#######
    Ordre by 排序:asc升序(默認(rèn)的) desc:降序 *放在sql語句的最后
    #######排序結(jié)束#######接下來是函數(shù)#######
    一、字符函數(shù)
    upper() 小寫字母變大寫 ·select upper('smith') from dual; 必須 加上from ·select * from emp where ename =upper('smith');
    lower() 大寫字母變小寫 ·select lower('HELLO WORLD') from dual;
    initcap() 開頭字母大寫 ·select initcap('HELLO WORLD') from dual;·select initcap(ename) from emp;
    *字符串除了可以使用||來連接。 還可以使用concat();函數(shù)來進(jìn)行連接·select concat('hellow','world') from dual;
    可以進(jìn)行字符串截取,求字符串長度。進(jìn)行指定內(nèi)容替換
    ·字符串截取:substr(); substr 的截取點是從0或者是1效果都是一樣的(Oracle)Oracle 中 可以輸入負(fù)值 來倒著截取。·select ename ,substr(ename,-3,3) from emp;
    ·字符串長度:length();
    ·內(nèi)容替換: replace();
    _________________范例_______________
    select substr('hello',1,3) 截取字符串,length('hello') 字符串長度,replace('hello','l','x') 字符串替換from dual;
    ________________________________________
    二、數(shù)值函數(shù)
    ·四舍五入 :round(); 可以指定四舍五入位數(shù)select round(789.546,-2) from dual;負(fù)值對整數(shù)進(jìn)行操作。 正值是小數(shù)
    ·截斷小數(shù)位 :trunc();
    ·取余(取模):mod
    三、日期函數(shù)
    ·日期-數(shù)字=日期
    ·日期+數(shù)字=日期
    ·日期-日期=數(shù)字(天數(shù))
    ·months_between();求出指定日期范圍的月數(shù)
    ·add_months();在制定日期加上制定的月數(shù),求出之后的日期
    ·next_day();下一個的今天是哪一個日期
    ·last_day();求出給定日期的月最后一天的日期
    當(dāng)前日期 sysdate關(guān)鍵字 范例: select sysdate from dual;
    四、轉(zhuǎn)換函數(shù)
    ·to_char(): 轉(zhuǎn)換成字符串
    ·通配符:·年:yyyy·月:mm·日:dd
    ·to_number(): 轉(zhuǎn)換成數(shù)字
    ·to_date(): 轉(zhuǎn)換成日期
    五、通用函數(shù)
    ·nvl(字段,0) 如果字段里面的值是空 就按照0顯示
    __________________________范例__________________________
    select empno,ename,(nvl(sal,0)+nvl(comm,0))*12 from emp;
    ________________________________________________________
    ·decode 類似if(){}else{}
    __________________________范例_________________________________
    1·select decode(1,1,'內(nèi)容是1',2,'內(nèi)容是2',3,'內(nèi)容是3') from dual;
    2·select empno 編號, ename 姓名 , HIREDATE 日期,decode
    (
    job,'CLERK','業(yè)務(wù)員','SALESMAN','銷售經(jīng)理',
    'MANAGER','經(jīng)理','ANALYST','分析員',
    'PRESIDENT','總裁'
    ) 職業(yè)
    from emp;
    _______________________________________________________________
    #######函數(shù)結(jié)束#######接下來是多表查詢#######
    1·基礎(chǔ)語句
    *在使用多表查詢的時候會出現(xiàn)笛卡爾積,如果表的數(shù)據(jù)越多,那么笛卡爾積也就會越大。
    比如一張表有1W條記錄,就有1W的五次方條記錄。(出現(xiàn)的原因是有關(guān)聯(lián)字段)
    *在多表查詢的時候加入where語句,就能消除笛卡爾積。
    一般會為表取別名 別名范例: select * from emp e ,dept d where e.deptno=d.deptno;
    ***范例(難題)**
    問題:求出雇員姓名,工資,部門名稱,工資等級,上級領(lǐng)導(dǎo)名字,領(lǐng)導(dǎo)的工資,領(lǐng)導(dǎo)的工資等級。
    __________________________________________________________________________________________
    select e.ename 雇員姓名,e.sal 工資,d.dname 部門名稱,decode(s.grade,'1','第五等工資','2','第四等工資','3','第三等工資','4','第二等工資' ,'5','第五等工資') 工資等級,m.ename 上級領(lǐng)導(dǎo)名字,m.sal 領(lǐng)導(dǎo)的工資,decode(ms.grade,'1','第五等工資','2','第四等工資','3','第三等工資','4','第二等工資' ,'5','第五等工資')領(lǐng)導(dǎo)的工資等級
    from emp e, dept d ,salgrade s ,emp m,salgrade ms
    where e.deptno =d.deptno and
    e.sal between s.losal and s.hisal
    and e.mgr=m.empno
    and m.sal between ms.losal and ms.hisal;
    __________________________________________________________________________________________
    *注意*
    有一個人沒有上級領(lǐng)導(dǎo),查不出,因為什么呢?因為所有人的最上層x領(lǐng)導(dǎo)是他!
    2·左右連接
    去除笛卡爾積的時候會讓某些字段不被查出。需要使用左右連接((+)用這個符號)
    例如select e.empno,e.ename,d.deptno,d.dname,d.loc from emp e,dept d where e.deptno(+)=d.deptno;
    表示右連接,以右邊的表為準(zhǔn),證明:(+)在左邊表示右連接,反之左連接。默認(rèn)左連接
    3·sql1999語法支持(了解即可)
    ·cross join : 交叉連接==>產(chǎn)生笛卡爾積
    ·natural join: 自然連接==>消除笛卡爾積
    ·using 字句: 直接關(guān)聯(lián)操作列
    _________________________范例_________________________________
    ·select * from emp e join dept d using(deptno) where deptno=30;
    on是自己定義條件·select * from emp e join dept d on(e.deptno=d.deptno) where e.deptno=30;
    __________________其結(jié)果都是一樣的____________________________
    ·左右連接(左右外連接)
    ·left join
    ·right join
    #######多表查詢結(jié)束#######接下來是組函數(shù)及分組統(tǒng)計#######*!重點!*
    1·組函數(shù)
    ·count(): 記錄數(shù)
    ·max(): 最大值\ >不能用在字符串
    ·min(): 最小值/
    ·avg(): 平均值
    ·sum(): 總和
    2·分組統(tǒng)計
    group by 分組條件
    __________________________范例_________________________
    ·select deptno,count(empno) from emp group by deptno;
    ·select deptno,avg(sal) from emp group by deptno;
    _______________________________________________________
    錯誤:_______________________________________________________
    1、使用了分組函數(shù),有兩種情況·使用了group by 并指定了分組條件 會將分組條件一起查詢出來·沒使用分組,就只能單獨使用分組函數(shù)。
    2、在使用分組函數(shù)的時候,不能出現(xiàn)分組函數(shù)和分組條之外的字段
    select d.dname ,count(e.empno) from dept d ,emp e
    where d.deptno=e.deptno
    group by d.dname;
    _______________________________________________________
    select max(avg(sal))
    from emp
    group by deptno;
    _______________________________________________________
    *!注意!*:分組函數(shù)值能在分組中使用不能在where中使用,要使用 having 關(guān)鍵字
    _____________________范例______________________________select deptno,avg(sal)from emp group by deptno having avg(sal)>2000;
    _______________________________________________________
    #######接下來是多表查詢######接下來是子查詢########
    子查詢是指,在以個查詢的內(nèi)部還包括另外一個查詢。
    *!注意!*:所有子查詢都要在()中編寫
    子查詢分為以下三類:
    ·單列子查詢:返回結(jié)果是一個列的一個內(nèi)容
    ·單行子查詢,返回多個列,有可能是一條完整的記錄
    ·多行子查詢,返回多條記錄
    ________________________范例__________________________________________
    --select * from emp where sal>(select sal from emp where empno=7654)and job=(select job fromemp where empno=7788);
    ----------------------------------------------------select d.dname,ed.c,ed.a,e.enamefrom dept d , (select deptno,count(empno) c, avg(sal)a,min(sal)minfrom empgroup by deptno) ed ,emp e<