在mysql中,我可以查询select * ... LIMIT 10, 30
,其中10表示要跳过的记录数。
有没有人知道我如何在删除语句中做同样的事情,前10条记录之后的每条记录都被删除了?
答案 0 :(得分:8)
考虑到MySQL中没有rowId
(就像在Oracle中一样),我建议如下:
alter table mytable add id int unique auto_increment not null;
这将自动按select
语句的顺序对行进行编号,无需条件或按顺序排序。
select * from mytable;
然后,在检查订单符合您的需求(可能是表的转储)之后
delete from mytable where id > 10;
最后,您可能想删除该字段
alter table mytable drop id;
答案 1 :(得分:3)
以下 NOT 工作:
DELETE
FROM table_name
WHERE id IN
( SELECT id
FROM table_name
ORDER BY --- whatever
LIMIT 10, 30
)
但这会:
DELETE
FROM table_name
WHERE id IN
( SELECT id
FROM
( SELECT id
FROM table_name
ORDER BY --- whatever
LIMIT 10, 30
) AS tmp
)
这也是:
DELETE table_name
FROM table_name
JOIN
( SELECT id
FROM table_name
ORDER BY --- whatever
LIMIT 10, 30
) AS tmp
ON tmp.id = table_name.id
答案 2 :(得分:0)
删除大量行时,这是一个有效的技巧:
CREATE TABLE new LIKE real; -- empty table with same schema
INSERT INTO new SELECT * FROM real ... LIMIT 10; -- copy the rows to _keep_
RENAME TABLE real TO old, new TO real; -- rearrange
DROP TABLE old; -- clean up.