Mysql on duplicate key update

   问题:我的数据库表test 有三个字段,id int(10),name char(12), testts timestamp; 如果我要我update name的时候,其对应的testts 也更新为最新的时间戳,要怎么设置?
 
   create table test{ id int(10) not null auto_increment, name char(12),testts timestamp}

   insert into test('id','name','testts') values(1,'Lydia',current_timestamp());

   insert into test ('id','name','testts') values(2,'Susan',current_timestamp()) on duplicate key update testts=current_timestamp();


  当使用update id=1 的数据时,其对应的timets的值是不会改变的。但是在update id=2的数据时,对应的timets 的值会变更成最新的时间戳。
   update test set name='Monica' where id=2;

on duplicate key update 如果有数据库中有要更新的数据,那么就更新数据库,如果没有的话就讲这条记录插入到数据库中。
 

猜你喜欢

转载自narycc.iteye.com/blog/1835790