从MySQL联接表中删除重复项

huangapple go评论117阅读模式
英文:

Delete duplicates from joined table MySQL

问题

请帮助我从 'Component' 表中删除所有具有相同 'country' 且属于相同 'Page' 的记录,只保留具有最新 createdAt 的国家的记录。

表之间的关系:
Page -> PageComponent.pageId -> PageComponent.componentId -> Component.id

带有表和数据的沙盒链接 https://www.db-fiddle.com/f/CnFouhFZyfkEpzGKij5eq/2

英文:

please help me delete from the 'Component' table all records that have the same 'country' and they belong to the same 'Page', leave only one record with the country that has the newest createdAt?

Relationship between tables:
Page -> PageComponent.pageId -> PageComponent.componentId -> Component.id

Sandbox with table and data https://www.db-fiddle.com/f/CnFouhFZyfkEpzGKij5eq/2

答案1

得分: 1

我认为我没有完全理解,但请尝试这个:

DELETE 
main 
FROM 
`Component` main 
WHERE main.id NOT IN (
SELECT MAX(c.`id`) 
FROM `Page` p 
JOIN `PageComponent` pc ON pc.`pageId`=p.`id`
JOIN (SELECT * FROM `Component`) c ON c.`id`=pc.`componentId`
GROUP BY c.`country`, p.`id` HAVING COUNT(*)>1
)

请告诉我是否有什么问题。

英文:

I think I am not getting it all but please try this:

DELETE 
main 
FROM 
`Component` main 
WHERE main.id NOT IN (
SELECT MAX(c.`id`) 
FROM `Page` p 
JOIN `PageComponent` pc ON pc.`pageId`=p.`id`
JOIN (SELECT * FROM `Component`) c ON c.`id`=pc.`componentId`
GROUP BY c.`country`, p.`id` HAVING COUNT(*)>1
)

Let me know if I have messed up something.

答案2

得分: 0

我找到了一个解决方案:

使用 duplicateIdTable 作为 (
选择
       任意值(c.country),
       在 (按 c.createdAt 排序) p.id, c.country 分区下的 LAG(c.id) 作为 duplicateComponentId,
       在 (按 c.createdAt 排序) p.id, c.country 分区下的 LAG(pc.id) 作为 duplicatePageComponentId,
       p.id
从 Page p
         加入 PageComponent pc ON p.id = pc.pageId
         加入 Component c ON pc.componentId = c.id
)
从 Component, PageComponent 删除 FROM Component
                                内连接 PageComponent ON PageComponent.componentId = Component.id
                                内连接 duplicateIdTable ON duplicateIdTable.duplicateComponentId = Component.id
                                和 duplicateIdTable.duplicatePageComponentId = PageComponent.id
   其中 Component.id = duplicateIdTable.duplicateComponentId
和 PageComponent.id = duplicateIdTable.duplicatePageComponentId
英文:

I found a solution:

WITH duplicateIdTable AS (
SELECT
       any_value(c.country),
       LAG(c.id) over (partition by p.id, c.country order by c.createdAt) as duplicateComponentId,
       LAG(pc.id) over (partition by p.id, c.country order by c.createdAt) as duplicatePageComponentId,
       p.id
FROM Page p
         JOIN PageComponent pc ON p.id = pc.pageId
         JOIN Component c ON pc.componentId = c.id
)
DELETE Component, PageComponent FROM Component
                                INNER JOIN PageComponent ON PageComponent.componentId = Component.id
                                INNER JOIN duplicateIdTable ON duplicateIdTable.duplicateComponentId = Component.id
                                and duplicateIdTable.duplicatePageComponentId = PageComponent.id
   WHERE Component.id = duplicateIdTable.duplicateComponentId
and PageComponent.id = duplicateIdTable.duplicatePageComponentId

huangapple
  • 本文由 发表于 2023年7月3日 20:19:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定