分類:計算機
為了做報表,經常對資料庫裡資料的操作,應該算是自己的一點小小的經驗.
在這裡為了方便敘述,表1 : 用a 表示 ;表2: 用b表示;
a: a1,a2,a3
b: b1,b2,b3
1.直連
select a.* ,b.* from a,b where a.a1=b.b1
作用:求兩個表中的交集.
2.左連或右連
select a.*,b.* from a,b where a.a1=b.b1(+) 或
select a.*,b.* from a,b where a.a1(+)=b.b1
作用: 由表a ,帶出表b中資訊
或由表b,帶出表a中資訊
3.並集
select a.*,b.* from a,b where a.a1=b.b1(+)
union
select a.*,b.* from a,b where a.a1(+)=b.b1
作用: 求兩個表之間的並集.
4.分組
select col1,col2,sum(col3) from a
group bycol1,col2
5.sql裡的switch
select
decode(col1,'男','n','女','y','nothing') from a
作用: 如果 表a 中col1裡的資料是 "男" ,那輸出"n",如果是 "女" ,則輸出"y",如果都不是那就輸出"nothing"
6.sql 裡的 if
select (
case when a>b then 'yes' else 'no' end) from a
作用: 如果a>b則輸出'yes' 否則輸出'no'
7.將多行相關資料做為一行輸出
例:表a 資料
a b c1
a b c2
a b c3
select col1, col2, max(col3)
from (select a.col1
,a.col2
,a.col3 || ',' || lag(a.col3, 1, null) over(partition by a.col1, a.col2 order by a.col3) || ',' || lag(a.col3, 2, null) over(partition by a.col1, a.col2 order by a.col3) as col3
from a)
group by col1, col2
輸出結果 : a b c3,c2,c1
Oracle 大表之間關聯update
declare maxrows number default 5000 row id table dbms sql.urowid table p id table dbms sql.varchar2 table cursor acnt first cur is select use hash t1,...
oracle表之間的關聯方式
oracle表之間的關聯方式多表之間的連線有三種方式 nestedloops,hash join 和 sort merge join.一 nested loop 對於被連線的資料子集較小的情況,巢狀迴圈連線是個較好的選擇。在巢狀迴圈中,內錶被外表驅動,外表返回的每一行都要在內表中檢索找到與它匹配的行...
oracle關聯表更新
如果有a表和b表,sql server中用 update a set field2 b.filed2 from a,b where a.field1 b.field1搞定,所以用慣了sql server的更新表語句,再用oracle的那真是特別的拗口。情景一 如果只是更新固定值,oracle和sql...