小弟请教高人,设有表如下--BOM
父件 子件 用量
a    x    1
a    y    2
b    z    3
b    r    1--订单
单号 BOM号 数量
1111 a     200
2222 b     300
3333 x     300
4444 z     320请写一个订单表中有父件订单而无子件订单的语句,谢谢了!

解决方案 »

  1.   

    select * from 订单表 d 
    where exists 
    (select 1 from bom b where b.父件=d.bom号)
      

  2.   


    --有父件订单而无子件订单,应该是bom号关联子件吧select * from 订单表 a
    where exists 
    (select 1 from bom b where b.bom号=a.子件)
      

  3.   

    with bom as(                                                 
      select 'a' parent, 'x' child, 1 num from dual union all                                                       
      select 'a' parent, 'y' child, 2 num from dual union all                                                       
      select 'b' parent, 'z' child, 3 num from dual union all                                                       
      select 'b' parent, 'r' child, 1 num from dual),
     ord as(
      select '1111' orderid, 'a' bom, 200 num from dual union all                                                       
      select '2222' orderid, 'b' bom, 300 num from dual union all                                                       
      select '3333' orderid, 'x' bom, 300 num from dual union all
      select '5555' orderid, 'y' bom, 300 num from dual union all
      select '4444' orderid, 'z' bom, 320 num from dual)
    SELECT *
      FROM ord, bom
     WHERE ord.bom = bom.parent AND
           NOT EXISTS (SELECT 1 FROM ord o WHERE o.bom = bom.child);
      

  4.   

    select * from 订单表 a 
    where exists 
    (select 1 from bom b where a.父件=b.bom号)