Select

需要幫助建構查詢以檢測多列上的重複項

  • March 11, 2021

我有下表

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',  
)

我想找到相同的所有 entity_idsattribute_id = 86相同store_id的位置value

例如對於列attribute_id, store_id, entity_id,value

86, 1, 1, mypath

86, 1, 2, mypath

但不與

86, 2, 1, mypath

結果將是entity_id, store_id,value

任何幫助表示讚賞。

您可以通過對您的store_idvalueCTE子查詢中進行分組以獲取受騙者然後將受騙者重新加入您的 catalog_product_entity_varchar表中來完成此操作,如下所示:

WITH CTE_Dupes AS
(
    SELECT store_id, value
    FROM catalog_product_entity_varchar
    WHERE attribute_id = 86
    GROUP BY store_id, value
    HAVING COUNT(1) > 1
)

SELECT C.entity_id, C.store_id, C.value
FROM catalog_product_entity_varchar C
INNER JOIN CTE_Dupes D
   ON C.store_id = D.store_id
   AND C.value = D.value
WHERE C.attribute_id = 86

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