分頁:
select * from (
select e.*, rownum from (
select * from emp
) e
where e.rownum > 5 and e.rownum < 10
)
type:
NUMBER(7,2) 總長度7位,小數2位
date 加天 hiredate + 1
加一個小時 hiredate + 1 / 24
oracle中空值和0:
select money, money + 100 from tb;
如果money是空值,加了100還是空值
別名:
select age, age+1 as new_age from user;
select age, age+1 as "new_age" from user;
加了雙引號,是什麼就是什麼, 可以加空格,特殊符號
||用來串連:列和列, 列和字串
顯示表結構,命令, 此命令只能在命令列下運行(僅限PL/SQL,其他不知)
select distinct name, age from user;
相當於distinct(name, age),所以name,age組合不重複就會顯示
預設格式DD-MON-RR 日月年
<>不等於 (!= 也ok的)
between x and y (含)
IN(set) 滿足set中出現的項就會出現
dual dual位oracle的虛表,常用於查詢常量或常量運算
函數:
concat('hello', 'world') 串連
substr('hello world', 1, 5) 取子串 第1-5個字元
lpad(str, 10, ch) 對於str, 如果長度小於10, 則在左側以ch輸入鍵台
lpad(str, 10, ch) 對於str, 如果長度小於10, 則在右側以ch輸入鍵台
length(str) 取str的長度
instr(str, ch) 查詢ch第一次出現在str是幾(從1開始, 沒有出現為0)
round(123.456, 2) 保留2位小數,四捨五入
round(date, 'MONTH') 精確到月份:如果天數超過一半(15,16是界限),則進到下個月1號,否則退到本月1號: DAY YEAR同理
trunc(123.456, 2) 保留2位小數
mod(123, 2) ==> 123%2
months_between(date1, date2) 計算兩個時間相差多少個月
add_months(hiredate, 3) 往hiredate上向後計算2個月
next_day(date, '星期一') 以date為起點,計算下一個星期一的日期
last_day(date) 對於date中的月份,它這個月中的最後一天的日期
nvl(a, b) 如果a為空白,則用b代替
nvl2(a, b, c) return (a is not null) b:c
nullif(a, b) 相等返回null,否則返回1
coalesce(a,b,c,d) 返回第一個不為空白的值
decode(job, 'CLERK', 'c',
'MANAGER', 'm',
'ANALYT', 'a',
'd') --如果job等於clerk顯示c,manager顯示m……
select avg(sal) from emp group by deptno
如果要帥選平均值大於2000的,則在後面加上: having avg全串連(sal) > 2000
不能使用where avg(sal) > 2000, oracle在文法上不支援
=====================================================================================
(+)方式的外串連只有oracle支援
sql 1999 文法支援多種不同的資料庫
select * from emp e, dept d where e.deptno(+) = d.deptno
select * from emp e left outer join dept d on (e.deptno = d.deptno)
select * from emp e right outer join dept d on (e.deptno = d.deptno)
select * from emp e full outer join dept d on (e.deptno = d.deptno) --全串連