mysql语法 insert values on duplicate key update where

新增时如果唯一列重复,则更新原始数据(与之重复的数据)

create or replace TABLE `t_user` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `user_code` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_as_cs NOT NULL COMMENT '账号(注意为员工的英文名)',
  `password` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_as_cs NOT NULL DEFAULT '88888888' COMMENT '密码',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `userCode` (`user_code`) USING BTREE COMMENT '账号唯一验证'
) ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_cs COMMENT='用 户信息表'

insert t_user(user_code,password,create_time) values('admin','aaaaabbc',now()) on duplicate key update 
user_code =  CONCAT(values(user_code), NOW()),
create_time = values(create_time)

select * from t_user;

注意:上述 insert t_user(user_code,password,create_time)的列中,需要有主键列或者唯一索引列

posted @ 2023-09-23 20:53  Journey&Flower  阅读(46)  评论(0编辑  收藏  举报