2012-10-11 24 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;