有3张表,A与B是主子表,通过partcode相连接,C表通过cgid与B表左连接,可是A表怎么办呢?
例如:
A表
aid     partcode        status1        001             702        002             103        003             50 B表bid    partcode         cgid         price1      001              b001         2.22      002              b002         3.03      003              b003         1.0
C表cid     cgid          qty    1       b001          202       b002          10
 
3       b003          304       b004          40 想实现的结果为cgid        price*qtyb001        44(因为status='70')b002        0 (因为status != '70')b003        0 (因为status != '70')b004        0 (因为status != '70')
  

解决方案 »

  1.   

    select b.cgid , b.price * c.qty from a , b, c where a.partcode = b.partcode and b.cgid = c.cgid and a.status = '70'
    union all
    select b.cgid , 0  from a , b, c where a.partcode = b.partcode and b.cgid = c.cgid and a.status <> '70'
      

  2.   

    最后的b00 0是因为C表里没有,才出那样的结果的select b.cgid , b.price * c.qty from a , b, c where a.partcode = b.partcode and b.cgid = c.cgid and a.status = '70'
    union all
    select b.cgid , 0  from a , b, c where a.partcode = b.partcode and b.cgid = c.cgid and a.status <> '70'
     
    所以是错误的 
     
      

  3.   

    select c.cgid,nvl(b.price*c.qty,0)
    from a join b
      on a.partcode=b.partcode and a.status=70
      right join c on b.cgid=c.cgid
      
      

  4.   

    那如果 C表还有条件呢,例如C表status='70'呢,如果加上的话,查询特别慢