2012-10-11 29 views
0

我正在寫一個選擇查詢,在哪裏條件我想檢查一個字段值,如果它爲空,那麼只有我會其他字段值。請注意,select用於遊標。選擇查詢不給予想要的結果

原始查詢:


SELECT ni.node_installation_id,ni.customer_node_id,ni.customer_id,c.brand_name_1,substr(nitr.name,1,instr(nitr.name, ' ')-1) as node_inst_type 
    from node_installation ni , customer c , node_inst_type_release nitr 
    where ni.customer_id = c.customer_id 
    and ni.node_inst_type_release_id = nitr.node_inst_type_release_id 
    **and trunc(sysdate) - trunc(ni.arne_timestamp) >= 60** 
    and ni.no_of_collection_node_missed >= 4 
    and c.customer_id =90; 

參考在大膽的聲明,如果ni.arne_timestamp爲空,結果也爲空。在這種情況下,我想檢查其他條件,如ni.arne_flag ='我',並採取選擇查詢這些行。希望這一次我很清楚。

+1

您在使用此產品cartisian from子句中使用左聯接,而不是 –

+1

滾裝原因是因爲你正在編寫古老的SQL。如果你使用ANSI92(1992!)語法,你可能會發現這個問題會神祕地消失,或者說明白。 – podiluska

回答

1

如果ni.arne_timestamp爲空,結果也爲空。在這種情況下,我想 檢查一樣ni.arne_flag其他條件=「I」

AND謂詞中添加NULL值的檢查,像這樣:

... 
AND(ni.arne_timestamp IS NULL OR ni.arne_flag = 'I') 
AND(ni.arne_timestamp IS NULL 
    OR 
    (trunc(sysdate) - trunc(ni.arne_timestamp)) >= 60) 

所以您的查詢,以在ANSI-92之後JOIN語法看起來應該像這樣:

SELECT 
    ni.node_installation_id, 
    ni.customer_node_id, 
    ni.customer_id, 
    c.brand_name_1, 
    substr(nitr.name, 1, instr(nitr.name, ' ') - 1) as node_inst_type 
FROM node_installation ni 
INNER JOIN customer c ON ni.customer_id = c.customer_id 
INNER JOIN node_inst_type_release nitr 
     ON ni.node_inst_type_release_id = nitr.node_inst_type_release_id 
WHERE (ni.arne_timestamp IS NULL OR ni.arne_flag = 'I') 
    AND (ni.arne_timestamp IS NULL 
     OR 
     (trunc(sysdate) - trunc(ni.arne_timestamp)) >= 60) 
    AND ni.no_of_collection_node_missed >= 4 
    AND c.customer_id = 90;