有两个表,order和order_detail,两表通过order_id相关联。我现在要删除order表中cust_id 为空的记录,以及order_detail中与之相关联的记录.

解决方案 »

  1.   

    可以先删除detail表中的相应的数据,然后再删除order表中的数据
      

  2.   

    1.笨的方法:可以先用select语句找出cust_id为空的字段,之后通过找出符合条件的记录查找到它的order_id,之后通过该order_id删除另一张表中的记录。
    2.简单的:可以用复合查询,a.order 与b.order_detail来查询。不过如果你不是很熟习逻辑机构的话,还是用第一种比较好一点。
      

  3.   

    delete from order_detail where order_id in (select order_id from order where cust_id is null)
    delete from order where cust_id is null
      

  4.   

    试试这个,你可以先把下面语句中“Delete”部分改为“Select * ”,
    看看是不是你想要删除的那些数据,再执行下面的语句以删除它们:Delete From order_Detail Where not exists (
    Select * from order a, order_detail b 
    Where a.order_id = b.order_id and a.cust_id not is null)Delete From order Where not exists (
    Select * from order a, order_detail b 
    Where a.order_id = b.order_id and a.cust_id not is null)注意,上面的语句也会将两表中 a.order_id <> b.order_id 这类异常数据删除掉!