简体   繁体   English

MySQL - 如果另一个表中存在id,则插入字段

[英]MySQL - Insert fields if id is present in another table

I have two tables, one called product_text with a products name, description and category. 我有两个表,一个名为product_text ,带有产品名称,描述和类别。 The other called product_price with the products selling_price and cost_price . 另一个名为product_price ,其产品为selling_pricecost_price

I want to update the product_price table with generic values, for example, the cost_price as 0.01 and the selling_price as 0.01 我想用通用值更新product_price表,例如, cost_price0.01selling_price0.01

I need to somehow insert this information into the product_price table for each row that exists in the product_text table. 我需要以某种方式插入此信息到product_price表中存在的每一行product_text表。

For example, if I have: 例如,如果我有:

id name     description category
1  example  example     hats
2  example2 example2    socks

in the product_text table, I want to insert: product_text表中,我想插入:

id selling_price cost_price
1  0.01          0.01
2  0.01          0.01

into the product_price table. 进入product_price表。

I want to do this for a million items, so need an efficient way of doing it. 我想为一百万件物品做这件事,所以需要一种有效的方法。

Please help 请帮忙

insert into product_price (id, selling_price, cost_price)
select id, 0.01, 0.01 from product_text

You can just update across a join like this: 您可以像这样更新连接:

UPDATE product_price AS pp
INNER JOIN product_text AS pt ON pp.id = pt.id
SET pp.cost_price = 0.01, pp.selling_price = 0.01

One note, looking at your table schema, it doesn't seem to make sense that these aren't just a single table. 一个注意事项,看看你的表模式,似乎没有意义,这些不仅仅是一个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM