Select
需要幫助建構查詢以檢測多列上的重複項
我有下表
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_ids
attribute_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_id
和value
在CTE或子查詢中進行分組以獲取受騙者然後將受騙者重新加入您的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