Mariadb

從表中選擇數據以插入/更新另一個類似的表

  • May 17, 2021

我有兩個表(在兩個數據庫中):

create table catalog_product_entity_text
(
   value_id int auto_increment comment 'Value ID'
       primary key,
   attribute_id smallint unsigned default 0 not null comment 'Attribute ID',
   store_id smallint unsigned default 0 not null comment 'Store ID',
   entity_id int unsigned default 0 not null comment 'Entity ID',
   value longtext null comment 'Value',
   constraint CATALOG_PRODUCT_ENTITY_TEXT_ENTITY_ID_ATTRIBUTE_ID_STORE_ID
       unique (entity_id, attribute_id, store_id),
   constraint CATALOG_PRODUCT_ENTITY_TEXT_STORE_ID_STORE_STORE_ID
       foreign key (store_id) references store (store_id)
           on delete cascade,
   constraint CAT_PRD_ENTT_TEXT_ATTR_ID_EAV_ATTR_ATTR_ID
       foreign key (attribute_id) references eav_attribute (attribute_id)
           on delete cascade,
   constraint CAT_PRD_ENTT_TEXT_ENTT_ID_CAT_PRD_ENTT_ENTT_ID
       foreign key (entity_id) references catalog_product_entity (entity_id)
           on delete cascade
)

create table catalog_product_entity_varchar
(
   value_id int auto_increment comment 'Value ID'
       primary key,
   attribute_id smallint unsigned default 0 not null comment 'Attribute ID',
   store_id smallint unsigned default 0 not null comment 'Store ID',
   entity_id int unsigned default 0 not null comment 'Entity ID',
   value varchar(255) null comment 'Value',
   constraint CATALOG_PRODUCT_ENTITY_VARCHAR_ENTITY_ID_ATTRIBUTE_ID_STORE_ID
       unique (entity_id, attribute_id, store_id),
   constraint CATALOG_PRODUCT_ENTITY_VARCHAR_STORE_ID_STORE_STORE_ID
       foreign key (store_id) references store (store_id)
           on delete cascade,
   constraint CAT_PRD_ENTT_VCHR_ATTR_ID_EAV_ATTR_ATTR_ID
       foreign key (attribute_id) references eav_attribute (attribute_id)
           on delete cascade,
   constraint CAT_PRD_ENTT_VCHR_ENTT_ID_CAT_PRD_ENTT_ENTT_ID
       foreign key (entity_id) references catalog_product_entity (entity_id)
           on delete cascade
)

我的_text表包含一些我想傳輸到_varchar表中的數據,但與主鍵沒有關聯。主鍵只是自動遞增。

從邏輯上講,這些表是由集合連接的(attribute_id, store_id, entity_id)

我想執行兩個操作:

  1. _text表中選擇特定屬性 ID 的所有值,例如
select cpet.attribute_id, cpet.store_id, cpet.entity_id, cpet.value
from catalog_product_entity_text cpet
where cpet.attribute_id in
     (<myids>)
 and cpet.value is not null;

現在對於上述結果中的每一行,我想: 2. 如果表中有對應(attribute_id, store_id, entity_id)_varchar,則更新該行。 3. 如果表中沒有對應項(attribute_id, store_id, entity_id),請_varchar使用源行中的數據添加一個新行。

如果我有一個共同的主鍵,我會使用REPLACE INTO,但在這種情況下,我認為它不合適。

如何建構這樣的查詢?它不需要是單個查詢。

以下是如何更新存在於 中的記錄catalog_product_entity_varchar並與 中的記錄相關的方法catalog_product_entity_text

UPDATE catalog_product_entity_varchar cpev
INNER JOIN catalog_product_entity_text cpet
   ON cpet.attribute_id = cpev.attribute_id
   AND cpet.store_id = cpev.store_id
   AND cpet.entity_id = cpev.entity_id
SET cpev.value = cpet.value 
WHERE cpet.attribute_id IN (<myids>)
 AND cpet.value IS NOT NULL;

完成更新後,您可以像這樣插入缺少的內容:

INSERT INTO catalog_product_entity_varchar (attribute_id, store_id, entity_id, value)
SELECT cpet.attribute_id, cpet.store_id, cpet.entity_id, cpet.value
FROM catalog_product_entity_text cpet
LEFT JOIN catalog_product_entity_varchar cpev
   ON cpet.attribute_id = cpev.attribute_id
   AND cpet.store_id = cpev.store_id
   AND cpet.entity_id = cpev.entity_id
WHERE cpet.attribute_id IN (<myids>)
 AND cpet.value IS NOT NULL
 AND cpev.attribute_id IS NULL; -- This last predicate ensures we only insert new records (i.e. the join didn't match any records in cpev to this record in cpet, if cpev.attribute_id is null here)

引用自:https://dba.stackexchange.com/questions/291693