有进货表一个,有商品资料表一个,有库存资料表一个,
怎样把进货表的进货数量写进库存表里?
假设商品资料表里有10个商品记录,但是,库存表里只有5个商品记录,
而进货表里却有6个记录,已有记录的增加库存数量就容易
而库存表里没有的记录,要怎样写SQL语句增加资料和库存啊?

解决方案 »

  1.   

    if EXISTS (select 1 from 库存表 where ...) 
       update ...
    else
       insert into ...
      

  2.   

    进货表 t1(Id,qty)
    商品资料表t2(Id)
    库存资料表t3(Id,qty)添加商品:
    Insert into t2(Id) select Id from t1 where not exists(select 1 from t2 where t1.Id=t2.Id)
    添加库存:
    Insert into t3(Id,qty) select Id,qty from t1 where not exists(select 1 from t3 where t1.Id=t3.Id)
      

  3.   

    进货表入库提交时,对库存进行查询判断,如果存在则直接修改update,否则insert一条新记录