MySQL用一个表中的字段更新另一个表中字段

MySQL用一个表中的字段更新另一个表中字段

原表

CREATE TABLE `src` (
  `id` int(11) NOT NULL COMMENT '假设store的id',
  `store_type` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在这里插入图片描述

目标表

CREATE TABLE `dst` (
  `id` int(11) NOT NULL,
  `store_type` varchar(255) DEFAULT NULL,
   `store_id` int(11),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在这里插入图片描述

现在想使用src的store_type更新dst的store_type字段三种方法:

update src, dst set dst.store_type = src.store_type where src.id = dst.store_id;

update dst set dst.store_type = (select store_type from src where src.id = dst.store_id);

update dst join src on dst.store_id = src.id set dst.store_type = src.store_type;

参考链接:

https://stackoverflow.com/questions/1293330/how-can-i-do-an-update-statement-with-join-in-sql


版权声明:本文为u011361138原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。