PS: Source table: t_source, target table: t_target
The first type
The fields of the source table and the target table are completely consistent
insert into t_target select * from t_source;
The second type
The fields of the source table and the target table are partially consistent, and only some fields of the source table are imported. to target table
insert into t_target(field 1, field 2, field 3, ...)
select field 1, field 2, field 3, ... from t_source; only needs to import data that does not exist in the target table
The third type
insert into t_target (field 1, field 2, ...)
SELECT field 1, field 2, ... FROM t_source
WHERE not exists (select * from t_target
where t_target. comparison field = t_source. comparison field);
Here is a complete example that shows how to update data from a source table named source_table to a target table named target_table:
-- 创建目标表
CREATE TABLE target_table (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
age INT
);
-- Determine the connection conditions
ALTER TABLE target_table ADD COLUMN source_table_id INT;
UPDATE target_table
SET source_table_id = source_table.id
FROM target_table
INNER JOIN source_table
ON target_table.name = source_table.name;
-- Update data
UPDATE target_table
INNER JOIN source_table
ON target_table.source_table_id = source_table.id< /span> The above is a simple example, you can modify and extend it according to the actual situation.
SET target_table.age = source_table.age;
Summary
Through the above steps, we can update data from one table to another table. First, we need to create a target table to receive data, then determine the join conditions between the source table and the target table, and finally use the UPDATE statement to update the data to the target table.
How to copy MySQL query results to a new table (update, insert)
In MySQL, query results can be copied to another table. There are usually two situations when copying, one is to update existing data, and the other is to insert a new record. This is illustrated below with examples. First build two test tables.
Table t1:
Table t2:
1. If the score value exists in the t2 table, update the score to the t1 table. Methods as below:
UPDATE t1,t2
SET t1.score = t2.score
WHERE t1.id = t2.id AND t2.score IS NOT NULL
This is to use the query results as conditions to update another table. Of course, t2 can also be a more complex query result rather than a specific table.
2. Update the username of the t1 table to the t2 table, and update the score of the t2 table to the t1 table. Methods as below:
UPDATE t1,t2
SET t1.score = t2.score,t2.username = t1.username
WHERE t1.id = t2.id
This method is actually similar to the above method. It can update the data of two tables at the same time, that is, copy and update part of the table data with each other.
3. Insert the query results of the t2 table into the t1 table. Methods as below:
INSERT INTO t1(id,username,score)
SELECT t2.id,t2.username,t2.score FROM t2 where t2.username = 'lucy'
The first two methods are to update the records of the table, and this method is to insert a new record. In fact, as can be seen from the script, this method combines the two steps of query and insertion into one.
Update one table with another table
1. Update a field
Method 1
update table1 set field1=table2.field1 from table2
where table1.id=table2.id
Method 2
Query and update the two tables using inner joins
UPDATE tableName1 t1
LEFT JOIN tableName2 t2 ON t2.id = t1.id
SET t1.lpEntityType2 = t2.lpEntityType2
Method 3
update A m,B mp set m.job_type = mp.job_type where mp.mobile= m.mobile;
2. Update multiple fields
Method 1
update B, A
set
B.username = A.username,
B.phone = A.phone
where
B.userId = A.userId Query and update the two tables using inner joins
Method 2
update
B join A on B.userId=A.userId
set
B.username = A.username,
B.phone = A.phone
MYSQL updates one table field to another table field
1、UPDATE m_node_device mnd SET mnd.enterprise_id = (SELECT md.enterprise_id FROM m_device md WHERE mnd.device_id = md.id)
2、-- 更新表字段为查询结果中的某一个字段
UPDATE m_device md,
(
SELECT
mi.img_url AS aa,
mi.device_id AS bb
FROM
m_device_img mi
INNER JOIN m_device_type_attr ma ON ma.id = mi.device_type_attr_id
WHERE
ma.attr_name = '设备铭牌'
) b
SET md.nameplate_img = b.aa
WHERE
md.id = b.bb
AND md.nameplate_img = '' and md.is_deleted = 0