找准目标,不断前行

MYSQL 关联更新

来一个测试表table1,(友情提醒,测试库执行脚本有时候也需要备份,所以修改sql为你的表后不要着急执行,先来个备份

DROP TABLE IF EXISTS `table1`;
CREATE TABLE `table1` (
  `id` int(11) NOT NULL,
  `name` varchar(64) DEFAULT NULL,
  `email` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of table1
-- ----------------------------
INSERT INTO `table1` VALUES ('1', '111', '111@163.com');
INSERT INTO `table1` VALUES ('2', '222', '333@163.com');
INSERT INTO `table1` VALUES ('3', '333', '222@163.com');
update table1 a,(select * from table1) b set a.`name` = CONCAT(b.`name`,b.email) where a.id = b.id
posted @ 2021-05-24 15:32  kevinZhuZhu  阅读(382)  评论(0编辑  收藏  举报