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

MySQL-如果另一个表中存在id,则插入字段,mysql,phpmyadmin,Mysql,Phpmyadmin,我有两个表,一个名为product\u text,其中包含产品名称、描述和类别。另一种称为product\u price,其中包含产品salling\u price和cost\u price 我想用通用值更新product\u price表,例如cost\u priceas0.01和selling\u priceas0.01 对于product\u text表中存在的每一行,我需要以某种方式将此信息插入product\u price表中 例如,如果我有: id name descript

我有两个表,一个名为
product\u text
,其中包含产品名称、描述和类别。另一种称为
product\u price
,其中包含产品
salling\u price
cost\u price

我想用通用值更新
product\u price
表,例如
cost\u price
as
0.01
selling\u price
as
0.01

对于
product\u text
表中存在的每一行,我需要以某种方式将此信息插入
product\u price
表中

例如,如果我有:

id name     description category
1  example  example     hats
2  example2 example2    socks
product\u text
表中,我想插入:

id selling_price cost_price
1  0.01          0.01
2  0.01          0.01
进入
产品价格

我想为一百万件物品做这件事,所以需要一种有效的方法


请提供帮助

您可以通过以下方式更新连接:

insert into product_price (id, selling_price, cost_price)
select id, 0.01, 0.01 from product_text
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

请注意,看看您的表架构,这些表不仅仅是一个表似乎没有意义。

您可以像这样跨连接进行更新:

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

请注意,看看您的表模式,这些表不仅仅是一个表似乎没有意义。

您想在product\u price中插入还是更新它?这会让你的处理方式有所不同。嗨,迈克,更新会很好。我可以清空表并进行插入,但如果有一种方法可以让我不必先清空表,那就太好了!:)您想在产品价格中插入或更新它吗?这会让你的处理方式有所不同。嗨,迈克,更新会很好。我可以清空表并进行插入,但如果有一种方法可以让我不必先清空表,那就太好了!:)Magento重新索引价格=2小时Juergen d的解决方案=0.1秒Magento重新索引价格=2小时Juergen d的解决方案=0.1秒