我正在尝试使用重复记录清理数据库。我需要将引用移动到一条记录并删除另一条记录。
我有两个表:Promoters 和 Venues,每个表都引用了一个名为ities 的表。问题是有同名不同id的城市,与场地和发起人有关系。
通过这个查询,我可以将所有发起人和场地与一个城市记录分组:
SELECT c.id as id, c.name as name, GROUP_CONCAT( DISTINCT p.id ) as promoters_ids, GROUP_CONCAT( DISTINCT v.id ) as venues_ids
FROM cities as c
LEFT JOIN promoters as p ON p.city_id = c.id
LEFT JOIN venues as v ON v.city_id = c.id
WHERE c.name IN ( SELECT name from cities group by name having count(cities.name) > 1 )
GROUP BY c.name
现在我想对发起人运行 UPDATE 查询,将 city_id 设置为上面查询的结果。
像这样的东西:
UPDATE promoters AS pr SET pr.city_id = (
SELECT ID
FROM (
SELECT c.id as id, c.name as name, GROUP_CONCAT( DISTINCT p.id ) as promoters_ids
FROM cities as c
LEFT JOIN promoters as p ON p.city_id = c.id
WHERE c.name IN ( SELECT name from cities group by name having count(cities.name) > 1 ) AND pr.id IN promoters_ids
GROUP BY c.name
) AS T1
)
我怎样才能做到这一点?
谢谢