2008-4-17上午
練習(xí)中所使用的表全為ORACLE安裝時(shí)所表的數(shù)據(jù)及表。
-- distinct 去掉重復(fù)的記錄
select distinct deptno,ename,sal from emp ;
--substr(str,start,len),截取字符串,STR需要截取的字符串或列,START為從第幾個(gè)字符開(kāi)始,LEN截取多長(zhǎng)
select substr(ENAME,2,2) from emp order by deptno asc,ename desc;
select chr(65) from dual; --將一個(gè)數(shù)轉(zhuǎn)換為字符
select ascii('A') from dual;--求一個(gè)數(shù)的ASCII碼
select round(23.652) from dual ;--四舍五入
select round(23.45902234,2) from dual;--四舍五入,后點(diǎn)小數(shù)2位
select to_char(sal,'$99,999.9999') from emp ;
--將一個(gè)數(shù)轉(zhuǎn)換為字符串并按某種格式,
--其中一個(gè)9代表一個(gè)數(shù)字,如果不夠位數(shù)取后面位,


select to_char(sal,'L99,999.9999') from emp ;--前面加上L,即Local加上本地字符串
select to_char(hiredate,'yyyy-mm-dd HH:mm:ss') from emp;
select to_char(sysdate,'yyyy-mm-dd hh24:mm:ss') today from dual ;
--日期轉(zhuǎn)換函數(shù)to_date(str1,str2) str1需要轉(zhuǎn)換的字符串,str2為轉(zhuǎn)換成什么格式
select * from emp where hiredate > to_date('1981-02-01','yyyy-mm-dd')
--將字符串轉(zhuǎn)換為數(shù)字to_number(str1,str2)str1需要轉(zhuǎn)換的字符串,str2為轉(zhuǎn)換成什么格式
select sal from emp where sal > to_number('$1,220.00','$99,999.9999')
--NULL情況處理,使用nvl(str1,str2),str1為需要處理的列,STR2為為空時(shí)默認(rèn)的值,如果為空時(shí)則為0,不為NULL時(shí)則直接為comm
select ename,nvl(comm,0) comm from emp ;
--四入五入到幾位,
select round(max(sal),2) 最大工資,round(min(sal),2) 最小工資,round(avg(sal),2) 平均工資 from emp ;
--將數(shù)字轉(zhuǎn)換為某種格式的字符串
select to_char(max(sal),'L9,999.99') 最大工資,to_char(min(sal),'L9,999.99') 最小工資,to_char(avg(sal),'L9,999.99') 平均工資 from emp ;
--group by分組查詢
select sal,deptno from emp group by deptno,sal;
--求所有員工中單個(gè)部門(mén)工資最高的員工所有信息
select A.* from emp A
inner join
(select deptno, max(sal) as total from emp group by deptno) B
on A.Deptno=B.deptno and A.Sal=B.total
