如何才能删除数据表中满足条件的后n条记录

wing_er 2003-10-16 07:00:36
请问用sql语句该怎么写?
...全文
74 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuixin13 2003-10-17
  • 打赏
  • 举报
回复
DELETE [LOW_PRIORITY] [QUICK] FROM table_name
[WHERE where_definition]
[ORDER BY ...]
[LIMIT rows]

DELETE 语句的LIMIT rows 选项是 MySQL 特有的,它告诉服务器在控制权被返回到客户端之前可被删除的最大记录行数目。这可以用来确保一个特定的 DELETE 命令不会占用太长的时间。
cloudchen 2003-10-17
  • 打赏
  • 举报
回复
mysql> create table test (
-> id int primary key not null auto_increment,
-> username varchar(15) not null
-> );

mysql> insert into test (username) values ('cloudchen');
mysql> insert into test (username) values ('mumac');
mysql> insert into test (username) values ('jordan');
mysql> insert into test (username) values ('michael');
mysql> insert into test (username) values ('michael');
mysql> insert into test (username) values ('michael');
mysql> insert into test (username) values ('michael');
mysql> insert into test (username) values ('michael');

mysql> delete from test where username = 'michael' order by id desc limit 2;

把username为michael的作后两条记录删掉

56,675

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧