Through a SQL statement, how to insert the data of one table into another table

     

Preface

     Sometimes, we often have this requirement: we need to query the data of certain fields in one table according to the conditions, and then insert it into another table. This is the transfer of data between tables.

     In this scenario, it is generally required to synchronize historical data.

     Assume that the two tables are A and B, and the table structure is different, but some fields have the same meaning.

     If there is less data, it's better to just enter it manually in the table where you need to insert the data. But if there are thousands of them, the manual method is definitely not acceptable. Please see the example below, how to achieve through sql.

1. Data preparation

1. Table structure

1) Create a help table t_help

CREATE TABLE `t_help` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` int(10) DEFAULT NULL COMMENT '用户ID',
  `problems` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '问题描述',
  `create_date` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`ID`)
)  ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='帮助表';

2) Create a reply form t_reply

CREATE TABLE `t_reply` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `help_id` int(10) NOT NULL COMMENT '帮助表_主键ID',
  `problems` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '问题描述',
  `reply` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '回复内容',
  `create_date` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='回复表';

2. Table data

1) Insert data into the help table t_help

At this time, the t_reply data in the reply table is empty, and no data is inserted

2. Demand

     I want to transfer the data in the help table t_help to the reply table t_reply. The corresponding relationship between the field values ​​is as follows:

    t_help table                             t_reply table

    problems          -->              problems

    id                      -->              help_id

    Current time --> create_date                                  

    Fixed value --> reply

 Description:

   In the t_reply table, the reply value is inserted into the fixed text "default one content", the value of create_date is inserted into the current time, and the remaining fields  correspond to the t_help table. 

Three, realization method

1.SQL template

INSERT INTO `目标表`(各个字段)
(
SELECT
	各个字段 或者 固定值
FROM
	来源表 
WHERE
	条件
	);

Description:

1) First, write the insert statement of the target table according to the normal writing method.

2) Generally, the first closing parenthesis) is followed by values, as shown below

INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....);

A pair of parentheses are directly followed here, and the query statement can be written in the parentheses.

3) The query statement writes the query of the source table.

2.SQL implementation

INSERT INTO `t_reply`(help_id,problems,reply,create_date)
(
SELECT
    id,
    problems,
    '默认一个内容',
    NOW( )
FROM
    t_help 
WHERE
    1 = 1
    );

Here, if it is a fixed string content, directly wrap it with single quotes, if it is the current time, directly use the NOW() function. The query statement does not need to add where 1=1. 

By executing the above statement, the data transfer from the t_help table to the t_reply table is completed~

At this time, the t_reply table data is not empty, and the table data is shown in the following figure:

 

Guess you like

Origin blog.csdn.net/u012660464/article/details/114014022