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