2011-03-04 33 views
8

Benim gereksinim karmaşık bir sorgudan bir sayı almak ve num = desiredNum olup olmadığını kontrol etmektir.Kademede koşullu seçim nasıl kullanılır?

o desiredNum eşitse, o zaman seçme tabloların başka bir set gerçekleştirmelisiniz,

Ben bir işlev yazma daha sorguda bu oldukça elde edebilirsiniz herhangi bir yolu var mı?

Örn:

select case when val =2 
then select val1 from table1 
else 'false' 
from (select val from table) 

bu mümkün mü ??

cevap

0

% 100 değilim Neye ihtiyacınız olduğunu anlıyorum. Başka, theValues ​​tablo sorgusu 2 dönerse, o zaman tablo2 gelen sonucundan sonuçlar elde Bu durumda "sihirli sayı" 2'dir

create table theValues (theValue integer) 
create table table1 (value1 integer) 
create table table2 (value2 integer) 


INSERT INTO theValues (thevalue) VALUES (2) 
INSERT INTO table1 (value1) VALUES (17) 
INSERT INTO table2 (value2) VALUES (8) 


SELECT value1 from table1 WHERE EXISTS (SELECT theValue from theValues WHERE theValue != 2) 
UNION ALL 
SELECT value2 from table2 WHERE EXISTS (SELECT theValue from theValues WHERE theValue = 2) 

: Ama bunu yapmak için bir birlik kullanabilirsiniz düşünüyorum seni val ve her ikisi de aynı tablodan val1 olduğunu geliyordu varsayalım Daniel

+0

Merhaba Daniel, benim dava koşulu sağlandığında, ben db başka seçme sorgusu çalıştırmak gerekir .. id bu, yalnızca bir kayıt her varsa mümkün ?? – swathy

13
select case when val=2 then val1 else val end as thevalue 
from table1 

, tablo 1'de

Cheers dan sonuç almak, ama val = 2 yerine val1 ne zaman kullanılacağını. Aslında iki tablo vardı ve her ikisi de daha sonra

select 
    case when val=2 
    then (select val1 from table1) 
    else 'false' 
    end 
from table 
+0

teşekkürler tht çalıştı .. :) !! – swathy

İlgili konular