数据库用到的两张表和表结构如下:需求:
当new_table插入一行数据时,如果new_table1有一行记录中的日期和物品是能在new_table中找到,则将new_table中符合这个日期和物品的所有记录的数量,合成材料1,合成材料2,合成材料3相加,合成材料总和是有合成材料1,合成材料2,合成材料3相加,最后再将所有合成材料3相加如果在new_table1中找不到和new_table中配料日期和物品相符合的记录,则将new_table新插入的那行数据赋值给new_table1,并计算合成材料总和(依然是合成材料1,合成材料2,合成材料3相加)总的意思是每个时刻,new_table1的同种物品同一天最多只能有一条记录请教一下各位,谢谢了

解决方案 »

  1.   

    补充下,有数据的那张表是new_table,没数据的是new_table1
      

  2.   

    当new_table插入一行数据时
    --------------------  说明触发器在 new_table 上
    则将new_table中符合这个日期和物品的所有记录的数量,合成材料1,合成材料2,合成材料3相加
    -------------------- 说明你要更新的仍然是 new_table
    MySQL 中不支持这样的行为
      

  3.   


    我可能说的不够明确,这里相加后的更新是指更新new_table1
      

  4.   

    -- 在日期和物品上建立唯一索引
    alter table new_table1 add unique(日期,物品);-- 在 new_table 上建立触发器
    create trigger tr_insert_new_table after insert on new_table for each row
    insert into new_table1(日期,物品,合成材料1,合成材料2,合成材料3)
    values(日期,物品,合成材料1,合成材料2,合成材料3
    on duplicate key update
    合成材料1=values(合成材料1)+合成材料1,
    合成材料2=values(合成材料2)+合成材料2,
    合成材料3=values(合成材料3)+合成材料3
    ;
      

  5.   

    少写了 new.
    -- 在日期和物品上建立唯一索引
    alter table new_table1 add unique(日期,物品);-- 在 new_table 上建立触发器
    create trigger tr_insert_new_table after insert on new_table for each row
    insert into new_table1(日期,物品,合成材料1,合成材料2,合成材料3)
    values(new.日期,new.物品,new.合成材料1,new.合成材料2,new.合成材料3)
    on duplicate key update
    合成材料1=values(合成材料1)+合成材料1,
    合成材料2=values(合成材料2)+合成材料2,
    合成材料3=values(合成材料3)+合成材料3
    ;