Mysql Updates certain fields of one table based on data from another of sql statement

  • 2021-10-25 08:10:14
  • OfStack

The following code shows you that Mysql updates some fields of one table according to the data of another table. The specific code is as follows:


DROP TABLE IF EXISTS T_U_TEMPLATE;
--  Template table 
CREATE TABLE T_U_TEMPLATE (
 ID     INT NOT NULL AUTO_INCREMENT comment ' Template table ID',
 TEMPLATE_CODE   VARCHAR(50) BINARY comment ' Template coding ',
 TEMPLATE_NAME   VARCHAR(300) BINARY comment ' Template name ',
 CREATE_TIME   datetime DEFAULT NULL COMMENT ' Creation time ',
 CREATE_BY varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT ' Founder ' ,
 UPDATE_BY varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT ' Updater ' ,
 UPDATE_DATE datetime COMMENT ' Update time ' ,
 constraint PK_U_TEMPLATE primary key (ID)
)DEFAULT CHARSET=utf8 comment ' Template table ';
DROP TABLE IF EXISTS TEMPLATE_TEMP_CREATE;
--  Template temporary table 
CREATE TABLE TEMPLATE_TEMP_CREATE (
 ID     INT NOT NULL AUTO_INCREMENT comment ' Template table ID',
 OBJECT_ID   VARCHAR(50) BINARY comment ' Template coding ',
 OPERATER_NAME varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT ' Updater ' ,
 CREATE_TIME datetime NOT NULL COMMENT ' Update time ' ,
 constraint PK_U_TEMPLATE primary key (ID)
)DEFAULT CHARSET=utf8 comment ' Template temporary table ';
--  Modify the creator creation time to T_U_TEMPLATE Table 
UPDATE T_U_TEMPLATE a,TEMPLATE_TEMP_CREATE b SET a.CREATE_TIME=b.CREATE_TIME WHERE a.TEMPLATE_CODE = b.OBJECT_ID;
UPDATE T_U_TEMPLATE a,TEMPLATE_TEMP_CREATE b SET a.CREATE_BY=b.OPERATER_NAME WHERE a.TEMPLATE_CODE = b.OBJECT_ID ;

Summarize


Related articles: