douya7121 2016-01-12 17:14
浏览 43
已采纳

如何从条件中删除2个表

I want to build a query to delete data from 2 tables but wiht some conditions like if the cree_facture.refe ='EST1' delete from cree_devis whith cree_devis.refe='EST1' and if the refe ='INV1' delete from cree_facture with the cree_facture.refe='INV1'

I try with this 2 query bu the problem delete all data from table not has the condition

DELETE FROM `cree_devis`, `cree_facture` 
USING `cree_devis`,`cree_facture` 
WHERE `cree_devis`.`cree_par` = `cree_facture`.`cree_par` 
  AND (`cree_devis`.`refe` = 'INV1'  OR `cree_facture`.`refe` = 'INV1' )

and

DELETE cree_devis, cree_facture 
FROM cree_devis 
INNER JOIN cree_facture ON (cree_devis.cree_par=cree_facture.cree_par)
WHERE cree_devis.refe = 'EST1' 
OR cree_facture.refe = 'EST1';
  • 写回答

1条回答 默认 最新

  • dragon8837 2016-01-12 18:10
    关注

    First of all you can not delete in 2 tables at the same time using a join.

    You probably want this:

    DELETE FROM CREE_DAVIS T1 WHERE T1.REFE = 'EST1' AND EXISTS (SELECT 1 FROM CREE_FACTURE T2 WHERE T1.CREE_PAR = T2.CREE_PAR AND T2.REFE = 'EST1')

    Explaining: Delete from Table 1 every row for which there exists a row with matching CREE_PAR in the other table, while both have REFE = 'EST1'

    Hope this is what you want.

    You can now also make the other delete in the same way, using INV1 and changing the table names in the inner and outer query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?