SQL - 从表中复制值并添加新值

问题描述 投票:0回答:1

我们有一个表格

catalog_category_entity_text
,其中包含以下列;

value_id - attribute_id - store_id - entity_id - value

例如以下数据;

6032 - 47 - 1 - 98 - 值文本示例

现在我们想要从 store 1 获取 attribute_id 47 的所有值,并使用下一个可用 value_id 设置值 id。然后 store_id 必须设置为 5,entry_id 和值应该从 store 1 复制。

如果 store_id 5 的值存在,则应覆盖它,否则应添加它。

因此应添加以下行:

6033 - 47 - 5 - 98 - 值文本示例

目前我们有以下代码,我们如何完成这个:

INSERT INTO catalog_category_entity_text(value, entity_id, attribute_id, store_id)
SELECT value, entity_id, 47 AS attribute_id, 1 AS store_id
FROM catalog_category_entity_text
WHERE store_id = 5
mysql
1个回答
0
投票

您的查询中的内容是倒退的。您要从中复制的行中的值应位于

WHERE
中,而您要分配的新值应位于
SELECT
中。

INSERT INTO catalog_category_entity_text(value, entity_id, attribute_id, store_id)
SELECT value, entity_id, attribute_id, 5 AS store_id
FROM catalog_category_entity_text
WHERE store_id = 1 AND attribute_id = 47
© www.soinside.com 2019 - 2024. All rights reserved.