oracle學習之基本查詢和條件過濾,分組函數使用


oracle是殷墟出土的甲骨文的第一個單詞,所以在中國叫做甲骨文,成立於1977年,總部位於美國加州。

在安裝好后,主要有兩個服務需要開啟:

1,實例服務,OracleServiceORCL,決定是否可以連接上數據庫

2,監聽服務,OracleOraDb11g_home1TNSListener,決定是否可遠程連接數據庫

在安裝好客戶端后,登錄方式:

sqlplus 用戶名/密碼

如果是遠程登錄,需要加上連接別名,或者IP:

sqlplus 用戶名/密碼@//IP地址/實例名
or
sqlplus 用戶名/密碼@連接名

數據庫基本查詢:

select * from tab;

tab是一個數據字典,字義了當前用戶有哪些表,以及類型,查看表結構:

desc dept

  注意,如果顯示行大小和寬度不正確,可以通過修改登錄設置文件改變:

路徑:\app\Administrator\product\11.2.0\client_1\sqlplus\admin
set linesize 140;//設置行大小
set pagesize 120;//設置頁寬度
col empno for 99999;//修改數值型字段長度
col ename for a20;//設置字符型字段長度

  sql語法的注意事項:關鍵字不能縮寫,不能換行,以分號結尾!

查看sqlplus的所有命令:

? topic

查詢SQL語句語法:

select [distinct]*|column_name|expr|alias from tablename

 查詢員工號,姓名,月薪,獎金,年薪:

注意,獎金字段有為null的結果,可以通過慮空函數解決,即:nvl(a,b),如果a為null,返回b,否則返回a

如果需要查看員工表不同的部門編號,並且去除重復的,可以這么做:

通常,我們還可以借助偽表來進行一些的數學計算或函數操作:

select 3+20*5,sysdate from dual;

  比較運算符的查詢條件過濾: = != <> < > <= >= (between and)

case1: 查詢入職日期為1981年11月17日的員工信息:

select * from emp where hiredate = '1981-11-17'

  這里要注意,如果顯示文字與格式字符串不匹配的問題,通常必須按照具體日期格式寫:

如果自己想修改這個日期格式,可以進入管理員用戶后修改,如下:

SQL> select sysdate from dual;
//修改會話日期格式
SQL> alter session set nls_date_format='yyyy-mm-dd';
//后面就可以這樣查詢了:
SQL> select * from emp where hiredate ='1981-11-17';

  邏輯運算符 and or not,這里特別說明一下,查找工資在1000~2000的員工信息:

select * from emp where sal >= 1000 and sal <= 2000;
//或者
select * from emp where sal between 1000 and 2000;

  between and 是屬於閉區間,從小到大!

如何正確查詢獎金為空的員工信息 - null

select * from emp where comm is null;

  當and or 存在多個條件寫,如何來寫才更優?

sql語句是從右到左執行,and情況,應該將易假的放在右側,or情況,應該將真的放在右側。

like ->模糊查詢,'%'匹配任意多個字符,'_'匹配任意一個字符

重點:查詢帶在下划線'_'的應該怎么查詢:

select * from emp where ename like '%/_%' escape '/';

  排序:group by; having; order by;語法如下:

order by col|expr|alias|number

  員工薪水按從大到小排序(序號)

select * from emp order by sal desc;
//或者
select * from emp order by 6 desc;
//或者
select empno,ename,job,mgr,sal,comm,deptno from emp order by 5 desc;

  這里更要注意null的情況,默認無窮大:

解決辦法是:

select * from emp where deptno = 30 order by comm desc nulls last;
//或者
select * from emp where deptno = 30 order by nvl(comm,-1) desc;

  oracle的單行函數:

什么叫單行函數,即對一行進行變換,只產生一個結果。

lower(小寫),upper(大寫),initcap(首字母大寫),||(可以進行字符串連接)

select lower('hello WorLD') one,upper('HeLLo woRld') two,initcap('hello WOrld') from dual;
select 'aaaa'||'bbbb'||'cccc' from dual;

  substr(字符串,位置,長度),從位置開始截取長度,長度可以省略,代表截取到未尾:

select substr('helloworld',1,3) one,substr('helloworld',1) two ,substr('helloworld',-3) three from dual;

  length求字符串長度,lengthb求字節長度:

instr(str1,str2) ,判斷str2是否在str1中,如果存在返回出現的首位置,否則返回0:

select instr('hello world','llo') from dual;

  lpad,rpad,左右填充l(r)pad(str,len,char) 返回len長度的字符串,如果str不夠長度,則是char字符填充:

select lpad('hello',10,'#') from dual;

  trim(str)去首尾空格,replace(str,substr,strto)替換

select 'aaa'||trim(' hello world ')||'bbb' from dual;

  還能這么玩:

重要:數值函數,round四舍五入,trunc截斷,mod取模

select round(45.926, 2) 一, round(45.926, 1) 二, round(45.926, 0) 三,  round(45.926, -1) 四, round(45.926, -2) 五 from dual;
select trunc(45.926, 2) 一, trunc(45.926, 1) 二, trunc(45.926, 0) 三,  trunc(45.926, -1) 四, trunc(45.926, -2) 五 from dual;
select mod(600,1000),mod(1000,600) from dual;
//ceil 和floor 向上取整,向下取整 
select ceil(121/60),floor(121/60) from dual;

 重要:轉換函數,to_char,to_number,to_date 

 

 把薪水轉換為本地貨幣字符型:

select sal,to_char(sal,'L9,999') from emp;

如果要將這個在轉回數值型,語句是一樣的:

select to_number('¥1,250','L9,999') from dual;

  to_char 與 to_date 顯示 'yyyy-mm-dd hh24:mi:ss 今天是 星期幾'

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss "今天是" day') from dual;

  將上述字符串反轉為日期:

select to_date('2017-11-10 15:48:13 今天是 星期五','yyyy-mm-dd hh24:mi:ss "今天是" day') from dual;

  此處可以解決一個隱式日期轉換問題:

select * from emp where to_char(hiredate,'yyyy-mm-dd')='1981-11-17';

 上面的能顯示轉換,盡量顯示轉換。

重要:日期函數,months_between add_months last_day next_day

1,顯示昨天,今天,明天:

select sysdate-1 昨天,sysdate 今天,sysdate+1 明天 from dual;

  2,計算員工工齡,可以按日,周,月,年,日期差減方法

select months_between(sysdate,to_date('2017-10-10','yyyy-mm-dd')) from dual;

  3,求明年的今天:

select add_months(sysdate,12) from dual;

  4,求指定日期所在月份最后一天

select last_day(sysdate) from dual;

  5,求指定日期的下一個星期幾日期:

select next_day(sysdate,'星期一') from dual;

  條件表達示

CASE expr  WHEN comparison_expr1 THEN return_expr1
            [WHEN comparison_expr2 THEN return_expr2
            WHEN comparison_exprn THEN return_exprn
            ELSE else_expr]
END

  老板打算給漲工資,總裁1000,經理800,其它400,將漲前,漲后根據職業列出:

select empno,ename,job,sal 漲前薪水,case job when 'PRESIDENT' then sal+1000
					when 'MANAGER' then sal+800
					else sal+400
					end 漲后薪水
from emp

另外在oracle中,還可以使用decode函數,其它Sql不支持:

語法:decode(expr,search1,res1,search2,res2,…,default)
select empno,ename,job,sal 漲前薪水,decode(job,'PRESIDENT',sal+1000,'MANAGER',sal+800,sal+400) 漲后薪水 from emp

分組函數:avg,sum,min,max,count

1,求員工的工資總和:

select sum(sal) from emp;

2,求員工的總人數:

select count(empno),count(*) from emp;

3,求emp表中工種數:

4,求平均工資(三種方式):

 

分組數據:在select中出現的非組函數的列,必須在group by中出現

1,查詢各部門平均工資:

select deptno,avg(sal) from emp group by deptno;

2,查詢各部門,各工種平均工資:

select deptno,job,avg(sal) from emp group by deptno,job;

3,查詢平均薪水大於2000的部門:

select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;

4,求10號部門員工的平均薪水:

select deptno,avg(sal) from emp where deptno=10 group by deptno;

  此處注意,第3和第4條,where后不能放分組函數,deptno不是,所以可以在where后

 where 是先過濾,后分組,having是先分組后過濾,所以相對來說where要好些。

常見的非法使用組函數情況:

 

最后對null進行一個總結:

1,null不能用 = 或者 !=,應該使用 is

2,not in的集合中不能有null

3,null無窮大,默認排在最前

4,表達式中null參與運算,結果為null,應使用nvl(a,b)

5,分組函數會自動過濾null

  

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM