如何在触发器上复制键后给出条件

时间:2013-01-02 13:06:34

标签: mysql sql navicat

以下代码是我在Navicat for Mysql中编写的触发器。插入部分工作正常但是为了更新,我需要根据id更新特定的行数量。如何在update sql查询中编写where子句。

insert into closingstockt(CLS_BSID,CLS_Qty,CLS_SQty,CLS_CDate)
select BS_ID as CLS_BSID,BS_Qty as CLS_Qty,BSS_Qty as CLS_SQty,curdate() 
from barstockt
where CLS_BSID=BS_ID 
on duplicate key update CLS_Qty=BS_Qty,CLS_SQty=BSS_Qty

1 个答案:

答案 0 :(得分:0)

ON DUPLICATE KEY UPDATE将更新数据库中已存在的行。例如:

INSERT INTO table (field,field2) VALUES ('data1','data2')
ON DUPLICATE KEY UPDATE lastupdate=NOW()

编辑:我建议使用IF()

INSERT ... ON DUPLICATE KEY UPDATE with WHERE?