我有一个表(数百万条记录),其中包含(dataid,url)的唯一索引。该表如下所示:
id dataid url
1 230 https://www.example.com/123
3 230 http://example.com/123
我无法运行查询
UPDATE table_name SET url = REPLACE(url, 'http://', 'https://www.')
因为存在重复和违反唯一键约束。在这种情况下,我想删除最大ID'值。我该怎么做?
答案 0 :(得分:0)
delete
from table a
join table b on a.dataid = b.dataid
where
a.dataid = 230 and a.id > b.id;
尝试一下
答案 1 :(得分:0)
这将找出应删除的行
select max(id), REPLACE(url, 'http://', 'https://www.') as url from table
group by REPLACE(url, 'http://', 'https://www.')
having count(*)>1
这将删除它们
delete t1 from table as t1 inner join
(
select max(id), REPLACE(url, 'http://', 'https://www.') as url from table
group by REPLACE(url, 'http://', 'https://www.')
having count(*)>1
) as t2 on t1.id=t2.id
现在更新您的数据
UPDATE table_name SET url = REPLACE(url, 'http://', 'https://www.')
答案 2 :(得分:0)
delete tst
where id in (select max(id)
from tst
group by dataid, REPLACE(url, 'http://', 'https://www.')
having count(*) = 2);
UPDATE tst SET url = REPLACE(url, 'http://', 'https://www.');
答案 3 :(得分:0)
首先你应该删除重复项。 此查询可以帮助您:
delete from
table_name
where id in (
select max(id)
from table_name
group by REPLACE(url, 'http://', 'https://www.')
having count(*) > 1
)