数据库的插入更新语句

来源:互联网 发布:淘宝海外物流怎么发货 编辑:程序博客网 时间:2024/05/18 00:24
目的:实现在数据库插入数据的时候,只对重复的数据进行更新;
实现方式:
1、在表中建立一个唯一索引,主键(已有唯一索引的特性)
2、在插入数据 sql语句:insert into table_name( 列名1,列名2)values(。。。)on duplicate key update 列名=values(值)或者(值)

这样子在出现于 唯一索引的值相同情况下 就会之更新其中的数据!

用例:
insert into tb_mx_baopantest
    (
      serv_node_name,
      not_report,
      reported,
      not_cancel,
      cancelled,
      part_dealed,
      dealed,
      part_cancelled,
      abolished,
      beReport,
      reporting,
      date,
      time
    ) values('1235',3,3,3,3,3,3,3,3,3,3,3,3)
    ON DUPLICATE KEY UPDATE
      not_report=VALUES(not_report),
      reported=VALUES(reported),
      not_cancel=VALUES(not_cancel),
      cancelled=VALUES(cancelled),
      part_dealed=VALUES(part_dealed),
      dealed=VALUES(dealed),
      part_cancelled=VALUES(part_cancelled),
      abolished=VALUES(abolished),
      beReport=VALUES(beReport),
      reporting=VALUES(reporting),
      date=VALUES(date),
      time=VALUES(time);

例子:
http://blog.csdn.net/lalaguozhe/article/details/9150049
原创粉丝点击