2017-09-15 90 views
-1

这只是一个有限制SQL:删除dupliacte行,除了一个有限制

DELETE n1 FROM v_news n1, v_news n2 WHERE n1.`id` > n2.`id` AND n1.`url` = n2.`url` ORDER BY n2.`id` LIMIT 100 

用于删除重复的行我的SQL查询,但我得到的错误是这样的:

MySQL said: Documentation 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY n2.`id` LIMIT 100' at line 1 

哪里是我的错误?

在此先感谢。

+0

我想在删除查询顺序和限制是适用的。 – Nidhi257

+0

可以删除“order by”。但是必须有一个限制。因为行数非常多。 – Jake

+0

orderby关键字与删除操作的用法给出错误。 –

回答

0

对于删除查询,不需要包含order bylimit。所以只要删除这两个并试图像下面,

DELETE n1 FROM v_news n1, v_news n2 WHERE n1.`id` > n2.`id` AND n1.`url` = n2.`url` 
+0

我认为关键是@Jake只想删除多达100行(不知道为什么,但大概是有一个基础逻辑)。 – JohnLBevan

+0

谢谢,我使用了限制,因为我的表中的行数是几百万,服务器无法完成请求。 – Jake

+0

这样你就可以在'for'循环中使用delete命令。它可能会工作 – KMS

0

由于documentation解释说:

您可以在DELETE语句中指定多个表中删除一个或多个表中根据病情行 在WHERE 条款。您不能在多表DELETE中使用ORDER BYLIMIT

换句话说,你的DELETE引用两个表(当然,同一个表两次),所以它被认为是一个多表删除。不允许使用ORDER BYLIMIT

注意:您应该使用JOIN而不是逗号来指定表格。

0

这种方法可能工作:

--use a temp table record the ids of the records you wish to delete 
create temporary table if not exists newsIdsToDelete 
(
    Id bigint 
    , PRIMARY KEY (id) 
) ENGINE=MEMORY; 

--populate that table with the IDs from your original query 
select n1.`Id` 
into newsIdsToDelete 
from v_news n1 
inner join v_news n2 
on n2.`id` < n1.`id` 
and n2.`url` = n1.`url` 
order by n2.`id` 
limit 100; 

--delete those records where the id's in the list of ids to be deleted 
delete v_news 
where `Id` in (
    select `Id` 
    from newsIdsToDelete 
); 

--clean up the temporary table 
drop temporary table if exists newsIdsToDelete; 

(我没有MySQL和我是DB的知识相当举步维艰,因此在使用时测试这个在安全的地方)。