2017-04-07 44 views
0

我有以下表的Oracle SQL不存在刪除我的所有記錄

product 
product_id 
parent_product_id 

product_details 
product_id 
percent 

和父表:

parent_product 
parent_product_id 
description_1 

parent_product_details 
parent_product_id 
percent 

我需要的是邏輯從那裏不存在product_detail刪除(在..從parent_product_detail數據)

然後

插入到product_detail WHE重新當我執行這個

delete from product_details pd 
where not exists 
    (select ppd.* 
    from parent_product pp, 
     parent_product pd, 
     product p, 
     parent_product_details ppd, 
     ,product_details pd 
     where pp.parent_product_id = '172' 
     and pd.parent_product_id = ppd.parent_product_id 
     and ppd.parent_product_id = pp.parent_product_id 
     and pd.product_id = p.product_id -- this filter 
) 
/

rollback 
/

1040 rows deleted 

Roll Back Completed 

不存在(從parent_product_details數據)

我的問題是,產品沒有任何產品的細節。所以在選擇的子查詢時/執行它顯示結果只有一個

select ppd.* 
     from parent_product pp, 
      parent_product pd, 
      product p, 
      parent_product_details ppd, 
      --,product_details pd 
      where pp.parent_product_id = '172' 
      and pd.parent_product_id = ppd.parent_product_id 
      and ppd.parent_product_id = pp.parent_product_id 
      --and pd.product_id = p.product_id -- this filter 

我如何去從product_details刪除記錄不再出現在parent_product_details,然後用刀片添加新的進入不存在的邏輯是什麼?

回答

2

我想你想要一個相關的子查詢。您還需要修復你的語法,使用正確的JOIN的語法,而不是古老的逗號,在最從子句的事情:

delete from product_details pd 
where not exists (select 1 
        from parent_product pp join 
         parent_product_details ppd 
         on ppd.parent_product_id = pp.parent_product_id join 
         parent_product mc 
         on mc.parent_product_id = ppd.component_parent_product_id 
         product p 
         on p.parent_product_id = pp.parent_product_id 
        where pp.parent_product_id = '172' and 
         pd.product_id = p.product_id -- this filter 
       ); 
+0

我認爲這個問題是該產品不含任何product_details所以它刪除這麼多行 –