MySQL - 从另一个表中插入与常量合并的数据 [英] MySQL - insert data from another table merged with constants

查看:16
本文介绍了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

这篇关于MySQL - 从另一个表中插入与常量合并的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆