MySQL - 从另一个表中插入与常量合并的数据

2022-01-09 00:00:00 select insert mysql

我有一个包含一些数据的临时表 (products_temp),我还有另一个表 (products) 我需要将数据插入其中.我需要在新记录上手动设置一些常量,例如 vendor_id=1 等...

I have a temporary table (products_temp) with some data, and I have another table (products) I need to insert the data into. I have some constants I need to set manually on new records, like vendor_id=1, etc...

是否可以在一个请求中插入临时表数据和常量?

Is it possible to do the insertion with the temporary table data and the constants in one request?

临时产品:

product_name | product_desc | category_name | mf_name ...

产品(category_name,mf_name 不在):

products (category_name, mf_name is not in):

product_id | product_name | product_desc | vendor_id | distributor_id ...

常量:

vendor_id=1, distributor_id=2

推荐答案

使用 INSERT ... SELECT 语句,您在其中选择常量值以及来自 products_temp 的数据:

Use an INSERT ... SELECT statement where you are selecting constant values as well as data from products_temp:

INSERT INTO products (product_data, vendor_id)
    SELECT data, '1' FROM products_temp

相关文章