|
8){NewaspContentLabel.style.fontSize=(--newasp_fontsize)+"pt";NewaspContentLabel.style.lineHeight=(--newasp_lineheight)+"pt";}‘> 減小字體 增大字體
在數(shù)據(jù)庫(kù)開(kāi)發(fā)的過(guò)程當(dāng)中,有很多時(shí)候需要將行轉(zhuǎn)換成列或者將列轉(zhuǎn)換成行來(lái)顯示數(shù)據(jù),而往往我們?cè)诮⒈斫Y(jié)構(gòu)時(shí)不能根據(jù)顯示的要求來(lái)保存數(shù)據(jù),于是乎只能在保存數(shù)據(jù)之后做一些必要的操作(比方說(shuō):建立視圖等)來(lái)達(dá)到顯示的目的。 下面用一個(gè)常見(jiàn)的數(shù)據(jù)顯示來(lái)說(shuō)明decode函數(shù)的用法。就是成績(jī)單的顯示,這個(gè)是教學(xué)管理系統(tǒng)中最常見(jiàn)的。我想做開(kāi)發(fā)的人員都遇到過(guò)這個(gè),而且在大學(xué)期間也是常常接觸成績(jī)單,顯示的是:姓名、語(yǔ)文、數(shù)學(xué)等 實(shí)現(xiàn)腳本如下(cjd.sql): --建表 create table stud ( sid varchar2(10), kcbm varchar2(10), cj int ); --插入測(cè)試數(shù)據(jù) insert into stud values(’1’,’語(yǔ)文’,80); insert into stud values(’2’,’數(shù)學(xué)’,90); insert into stud values(’3’,’英語(yǔ)’,100); commit; --創(chuàng)建視圖,decode用法 create or replace view cjd as select sid, decode(kcbm,’語(yǔ)文’,cj,0) 語(yǔ)文, decode(kcbm,’數(shù)學(xué)’,cj,0) 數(shù)學(xué), decode(kcbm,’英語(yǔ)’,cj,0) 英語(yǔ) from stud order by sid; --顯示數(shù)據(jù) select * from cjd; 執(zhí)行過(guò)程如下: SQL> create table stud(sid varchar2(10), 2 kcbm varchar2(10), 3 cj int); 表已創(chuàng)建。 SQL> insert into stud values(’1’,’語(yǔ)文’,80); 已創(chuàng)建 1 行。 SQL> insert into stud values(’2’,’數(shù)學(xué)’,90); 已創(chuàng)建 1 行。 SQL> insert into stud values(’3’,’英語(yǔ)’,100); 已創(chuàng)建 1 行。 SQL> commit; 提交完成。 SQL> create or replace view cjd as 2 select sid, 3 decode(kcbm,’語(yǔ)文’,cj,0) 語(yǔ)文, 4 decode(kcbm,’數(shù)學(xué)’,cj,0) 數(shù)學(xué), 5 decode(kcbm,’英語(yǔ)’,cj,0) 英語(yǔ) 6 from stud 7 order by sid; 視圖已建立。
SQL> select * from cjd;
SID 語(yǔ)文 數(shù)學(xué) 英語(yǔ) ---------- ---------- ---------- ---------- 1 80 0 0 2 0 90 0 3 0 0 100
|