oracle的if語句采用decode函數(shù)。
創(chuàng)新互聯(lián)長期為數(shù)千家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為寶應企業(yè)提供專業(yè)的網(wǎng)站設計制作、網(wǎng)站設計,寶應網(wǎng)站改版等技術服務。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
DECODE(value,if1,then1,if2,then2,if3,then3,...,else)
表示如果value 等于if1時,DECODE函數(shù)的結果返回then1,...,如果不等于任何一個if值,則返回else。
Oracle數(shù)據(jù)庫是對標準sql語言的過程化擴展,因此產(chǎn)生了pl/sql語言。其中的if語句大量使用使得程序模塊化的功能方便實用?,F(xiàn)在要討論的是if語句的基本使用方法。
連接數(shù)據(jù)庫
請輸入用戶名: ?scott/123456
設置環(huán)境變量
SQL set serveroutput on
定義兩個字符串變量,然后賦值,接著使用if……then語句比較兩個字符串變量的長度,并輸出比較結果。
declare
a varchar(10);
b varchar(10);
begin
a:='beijing';
b:='guangdong';
if length(a)length(b)
then dbms_output.put_line('ab');
end if;
end;
過if……then……else語句實現(xiàn)只有年齡大于等于56歲,才可以申請退休,否則程序會提示不可以申請退休。
declare
a number(10);
begin
a:=x;
if a=56
then dbms_output.put_line('可以申請退休');
else dbms_output.put_line('不可以申請退休');
end if;
end;
制定一個月份數(shù)值,然后使用if……then……elsif語句判斷它所屬的季節(jié),并輸出季節(jié)信息。
declare
mon number(10);
begin
mon:=x;
if mon=3 or mon=4 or mon=5
then dbms_output.put_line('春節(jié)');
elsif mon=6 or mon=7 or mon=8 then dbms_output.put_line('夏季');
elsif mon=9 or mon=10 or mon=11 then dbms_output.put_line('秋季');
elsif mon=12 or mon=1 or mon=2 then dbms_output.put_line('冬季');
end if;
end;
制定一個季度數(shù)值,然后使用case語句判斷它所包含的月份信息并輸出。
declare
ss number(10);
begin
ss:=x;
case
when ss=1 then dbms_output.put_line('包含月份3,4,5');
when ss=2 then dbms_output.put_line('包含月份6,7,8');
when ss=3 then dbms_output.put_line('包含月份9,10,11');
when ss=4 then dbms_output.put_line('包含月份12,1,2');
end case;
end;
建表,測試數(shù)據(jù):
create?table?test
(收款標志?int)
insert?into?test?values?(1);
insert?into?test?values?(1);
insert?into?test?values?(1);
commit;
執(zhí)行:
select?case
when?a.cnt?=?b.cnt?then
'未收款'
when?a.cnt?=?d.cnt?then
'已收款'
when?c.cnt??0?then
'部分收款'
end?收款狀態(tài)
from?(select?count(*)?cnt?from?test)?a,
(select?count(*)?cnt?from?test?where?收款標志?=?1)?b,
(select?count(*)?cnt?from?test?where?收款標志?=?2)?c,
(select?count(*)?cnt?from?test?where?收款標志?=?3)?d
結果:
然后你自己換點其他數(shù)據(jù)測試一下吧,思路就這么個思路了。
SELECT
distinct?id,state,name
FROM
table1?main
WHERE
NOT?EXISTS(?select?1?FROM?table1?sub?where?main.id=sub.id?AND?main.statesub.state);
未經(jīng)測試。。。純屬手寫,,如果以自己多年經(jīng)驗來說的話。。這段話應該不會有多大問題。。。希望你自己仔細測試之后能夠提出寶貴意見?。?!
好像是標準sql吧,就這么寫啊。不過大表可不能這么做哦,太占資源了。
補充:
oracle里面有“+”的,不過我懷疑你是不是要拼兩個字符串。正統(tǒng)數(shù)據(jù)庫,包括oracle和db2拼接字符串都是采用雙豎線“||”,加號只能用于使兩個整型或者浮點型數(shù)值相加。
這需要看你的相關字段的類型的。如果是數(shù)值型,需要首先轉換為字符型,再合并,例如:
select
*
from
a
where
to_char(col001)||to_char(col002)
not
in
(select
to_char(col001)||to_char(col002)
from
b)
如果是字符型,可以直接合并:
select
*
from
a
where
col001||col002
not
in
(select
col001||col002
from
b)
如果是date型,同樣轉換為字符,具體查手冊。
但是你這種寫法,怎么說呢,不太好把,首先這并不是嚴格按照你所描述的邏輯,舉例來說,如果表a字段是:"12","3",表b是:"1","23"那又會怎樣?另外,not
in總是執(zhí)行全表掃描,效率不高,這樣寫會好一些:
select
a.*
from
a
left
join
b
on
(a.col001
=
b.col001
and
a.col002
=
b.col002)
where
b.col002
is
null
是存儲過程里面的 IF/ELSE ? 還是簡單的 DECODE ?
SQL DECLARE
2 testvalue INT;
3 BEGIN
4 testvalue := 100;
5
6 IF testvalue 100 THEN
7 dbms_output.put_line( '100+' );
8 ELSIF testvalue = 100 THEN
9 dbms_output.put_line( '100' );
10 ELSE
11 dbms_output.put_line( '100-' );
12 END IF;
13
14 END;
15 /
100
PL/SQL procedure successfully completed.
SQL SELECT
2 DECODE(GROUPING(sale_item), 1, 'ALL', sale_item) AS iten,
3 SUM(sale_money) AS money
4 FROM
5 sale_report
6 GROUP BY
7 ROLLUP(sale_item);
ITEN MONEY
------ ----------
A 733285
B 2382
C 5738
ALL 741405
分享文章:oracle里判斷怎么寫 oracle判斷以什么開頭
URL標題:http://muchs.cn/article6/hjcjig.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供Google、、商城網(wǎng)站、移動網(wǎng)站建設、小程序開發(fā)、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)