我有一个表(数百万条记录)与(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‘值最大的记录。我该怎么做呢?
发布于 2016-04-22 15:23:54
delete
from table a
join table b on a.dataid = b.dataid
where
a.dataid = 230 and a.id > b.id;试试看
发布于 2016-04-22 15:26:51
这将找出应该删除的行
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.')发布于 2016-04-22 15:40:21
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.');https://stackoverflow.com/questions/36787436
复制相似问题