Mysql inserts the query results and simulated field data into the specified table in batches

Scenes:

we have 2 tables


 

Product table: 

Product expansion table:
 

We need to wash all the data with status=1 in the product table into the expansion table, and sn is our fixed data, and extra is also fixed data.


sn = 10011

 

extra = ‘test’

   

sql:

① Construct and get the data set that needs to be inserted in batches
 

SELECT id AS product_id, '1001' AS sn, 'test' AS extra

FROM product WHERE STATUS=1

Query effect:

 

 

 ② Insert the query results into the product_extra table in batches:
 


INSERT INTO product_extra 
(product_id,sn,extra)

SELECT id AS product_id, '1001' AS sn, 'test' AS extra

FROM product WHERE product.STATUS=1

result:
 

Data batch insertion succeeded

 

 

Guess you like

Origin blog.csdn.net/qq_35387940/article/details/130156141