这是我的sql查询,用于删除重复行,但限制为
的行除外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
但我得到的错误是这样的:
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 :(得分:0)
对于删除查询,无需包含order by
和limit
。所以只需删除这两个,然后尝试如下,
DELETE n1 FROM v_news n1, v_news n2 WHERE n1.`id` > n2.`id` AND n1.`url` = n2.`url`
答案 1 :(得分:0)
正如documentation所解释的那样:
您可以在
DELETE
语句中指定多个表来删除行 来自一个或多个表格,具体取决于WHERE
中的条件 条款。您无法在多表ORDER BY
中使用LIMIT
或DELETE
。
换句话说,你的DELETE
引用了两个表(好吧,同一个表两次),所以它被认为是多表删除。 1}}和ORDER BY
是不允许的。
注意:您应该使用LIMIT
而不是逗号来指定表格。
答案 2 :(得分: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,而且我对该数据库的了解非常严格,所以请在使用前在安全的地方进行测试。)