select count(*) from (
select ta.drug_code,tb.item_code from outp_presc_t ta , outp_orders_costs tb
where ta.serial_no = tb.serial_no (+)
and ta.order_no = tb.order_no (+)
and ta.sub_order_no = tb.order_sub_no (+)
and ta.item_class IN ('A','B')
and tb.item_class IN ('A','B')
and ta.visit_date > sysdate - 60 and ta.visit_date < sysdate - 1) aaselect count(*) from (
select ta.drug_code,tb.item_code from (select * from outp_presc_t where visit_date > sysdate - 60 and visit_date < sysdate - 1 and item_class in  ('A','B') ) ta , (select * from outp_orders_costs where item_class in ('A','B')) tb
where ta.serial_no = tb.serial_no (+)
and ta.order_no = tb.order_no (+)
and ta.sub_order_no = tb.order_sub_no (+)
) aa我觉得是一样的,但最后搜索出来的结果却不一样,请问有什么不同啊??

解决方案 »

  1.   

    and tb.item_class(+) IN ('A','B')
    第一个改这样试试
      

  2.   

    select count(*) from (
    select ta.drug_code,tb.item_code from outp_presc_t ta , outp_orders_costs tb
    where ta.serial_no = tb.serial_no (+)
    and ta.order_no = tb.order_no (+)
    and ta.sub_order_no = tb.order_sub_no (+)
    and ta.item_class IN ('A','B')
    and tb.item_class IN ('A','B')
    and ta.visit_date > sysdate - 60 and ta.visit_date < sysdate - 1) aa
    等价于
    select count(*) from (
    select ta.drug_code,tb.item_code from outp_presc_t ta , outp_orders_costs tb
    where ta.serial_no = tb.serial_no (+)
    and ta.order_no = tb.order_no (+)
    and ta.sub_order_no = tb.order_sub_no (+)
    ) aa where  ta.item_class IN ('A','B')
    and tb.item_class IN ('A','B')
    and ta.visit_date > sysdate - 60 and ta.visit_date < sysdate - 1
    可以理解成先进行外连接,再对连接后的结果进行过滤,而你的第二条语句是先过滤了单表结果,再进行外连接,所以结果会不一样
      

  3.   

    关联条件符合的条件下,符合ta的假设有10条, 符合tb的假设有8条
    第一种关联, 结果应该为8条, tb作为个外联表,这种写法,外联就失效了,相当于直连了,可以参考楼上的说法理解。
    第二种关联,结果应该是10条 ,tb作为个外联表,仍然保持外联关系,条数不会少。
      

  4.   

    原因就是第一段中的这句
    select count(*) from (
    select ta.drug_code,tb.item_code from outp_presc_t ta , outp_orders_costs tb
    where ta.serial_no = tb.serial_no (+)
    and ta.order_no = tb.order_no (+)
    and ta.sub_order_no = tb.order_sub_no (+)
    and ta.item_class IN ('A','B')
    and tb.item_class IN ('A','B')
    and ta.visit_date > sysdate - 60 and ta.visit_date < sysdate - 1) aa因为你的tb表是左联的,如果tb.item_class IN ('A','B')不满足条件,也应该查询出数据。
    但查询条件像上面的写法,如果tb.item_class IN ('A','B')不满足条件,就查询不出数据。