2017-04-07 12 views
0

I持って、次の表の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からのデータ)が存在しない

再私が持っている問題は、製品がどの製品の詳細を持っていないということです。だから、ときサブクエリが選択されている/それを実行すると、1つの結果だけを示し

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

相関サブクエリと思っています。私は、問題はそれが非常に多くが削除されますので、製品がどのproduct_detailsが含まれていないということだと思います

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

:あなたはまた、古風なコンマ・イン・-から節の事、正しいJOIN構文を使用するように構文を修正する必要はありません行 –

関連する問題