现有如下下面两个
d_dapiaojl(订单表)
订单号       订单总吨数       已发吨数     剩余吨数
f1                f2              f3           f4
201112001        100             100           0
201112002         50              50           0
201112003         80              80           0
201112004         50              50           0
201112005         60              40          20d_zhongchej(出库单表)(有些出库单需要两张订单合并发货)
出库单号  出库总数   订单号1   订单号1吨数  订单号2   订单号2吨数
f1          f2          f3           f4        f5        f6
0001        20       201112001      20
0002        80       201112001      80
0003        20       201112002      20
0004        80       201112002      30     201112003    50
0005        30       201112003      30 
0006        20       201112004      20       
0007        60       201112004      30     201112005    30
0008        10       201112005      10 现在希望删除d_dapiaojl(订单表)和 d_zhongchej(出库单表)的部分记录
要求  
1、d_dapiaojl(订单表)已发完货即f4=0
2、d_zhongchej(出库单表)对应的记录也要删除,但要考虑订单号2不为空时,对应订单没发完的情况不能删除
如0007因201112005也发货30吨,且201112005没发货完毕所以不能删除0007,所以201112004也不能删除删除后的记录如下
d_dapiaojl(订单表)
订单号       订单总吨数       已发吨数     剩余吨数
f1                f2              f3           f4
201112004         50              50           0
201112005         60              40          20d_zhongchej(出库单表)
出库单号  出库总数   订单号1   订单号1吨数  订单号2   订单号2吨数
f1          f2          f3           f4        f5        f6
0006        20       201112004      20       
0007        60       201112004      30     201112005    30
0008        10       201112005      10 求上述两个删除语句如何写
    

解决方案 »

  1.   

    delete a from d_zhongchej a inner join d_dapiaojl b on a.订单号1=b.订单号 inner join d_dapiaijl c on a.订单号2=c.订单号
    where b.剩余吨数=0 and c.剩余吨数=0
    go
    delete from d_dapiaojl a where not exists(select 1 from d_zhongchej where 订单号1=a.订单号)
    and not exists(select 1 from d_zhongchej where 订单号2=a.订单号)
    go
      

  2.   

    delete a from d_zhongchej a inner join d_dapiaojl b on a.订单号1=b.订单号 inner join d_dapiaijl c on a.订单号2=c.订单号
    where b.剩余吨数=0 and c.剩余吨数=0
    go
    delete from d_dapiaojl a where not exists(select 1 from d_zhongchej where 订单号1=a.订单号)
    and not exists(select 1 from d_zhongchej where 订单号2=a.订单号)
    go
      

  3.   

    不行呀,
    用1、select *  from d_zhongchej a inner join d_dapiaojl b on a.订单号1=b.订单号 inner join d_dapiaijl c on a.订单号2=c.订单号
    where b.剩余吨数=0 and c.剩余吨数=0 
    只查询到0004一条记录
    用select * from d_dapiaojl a where not exists(select 1 from d_zhongchej where 订单号1=a.订单号)
    and not exists(select 1 from d_zhongchej where 订单号2=a.订单号)
    查询不到一条记录
      

  4.   

    纠正:
    create table d_dapiaojl(订单号 varchar(15),订单总吨数 int,已发吨数 int,剩余吨数 int)
    insert into d_dapiaojl select '201112001',100,100,0
    insert into d_dapiaojl select '201112002',50,50,0
    insert into d_dapiaojl select '201112003',80,80,0
    insert into d_dapiaojl select '201112004',50,50,0
    insert into d_dapiaojl select '201112005',60,40,20
    create table d_zhongchej(出库单号 varchar(10),出库总数 int,订单号1 varchar(15),订单号1吨数 int,订单号2 varchar(15),订单号2吨数 int)
    insert into d_zhongchej select '0001',20,'201112001',20,null,null
    insert into d_zhongchej select '0002',80,'201112001',80,null,null
    insert into d_zhongchej select '0003',20,'201112002',20,null,null
    insert into d_zhongchej select '0004',80,'201112002',30,'201112003',50
    insert into d_zhongchej select '0005',30,'201112003',30,null,null
    insert into d_zhongchej select '0006',20,'201112004',20,null,null
    insert into d_zhongchej select '0007',60,'201112004',30,'201112005',30
    insert into d_zhongchej select '0008',10,'201112005',10,null,null
    go
    delete a from d_dapiaojl a 
    where 剩余吨数=0 and not exists(
    select * from d_zhongchej where 订单号1=a.订单号 and 订单号2 in(select 订单号 from d_dapiaojl where 剩余吨数>0)
     or 订单号2=a.订单号 and 订单号1 in(select 订单号 from d_dapiaojl where 剩余吨数>0)
    )
    go
    delete a from d_zhongchej a where not exists(select 1 from d_dapiaojl where 订单号=a.订单号1 or 订单号=a.订单号2)
    /*
    出库单号       出库总数        订单号1            订单号1吨数      订单号2            订单号2吨数
    ---------- ----------- --------------- ----------- --------------- -----------
    0006       20          201112004       20          NULL            NULL
    0007       60          201112004       30          201112005       30
    0008       10          201112005       10          NULL            NULL(3 行受影响)订单号             订单总吨数       已发吨数        剩余吨数
    --------------- ----------- ----------- -----------
    201112004       50          50          0
    201112005       60          40          20(2 行受影响)
    */
    go
    select * from d_zhongchej
    select * from d_dapiaojl
    go
    drop table d_dapiaojl,d_zhongchej
      

  5.   

    不行呀,
    用1、select *  from d_zhongchej a inner join d_dapiaojl b on a.订单号1=b.订单号 inner join d_dapiaijl c on a.订单号2=c.订单号
    where b.剩余吨数=0 and c.剩余吨数=0 
    只查询到0004一条记录
    用select * from d_dapiaojl a where not exists(select 1 from d_zhongchej where 订单号1=a.订单号)
    and not exists(select 1 from d_zhongchej where 订单号2=a.订单号)
    查询不到一条记录