我有一个MYSQL数据库正在运行以进行一些测试,即从网页获取表'结果'的值。它包含一些行和列。
当我尝试:
select * from results limit 10;
显示某些表的数据库中的前10行。
基本上,我想删除这些行。当我尝试:
delete from results where (select * from results limit 10) ;
它不会删除这些行,它会问:
delete from results where (select * from results limit 10) ;
也尝试过:
mysql> delete * from results where (select * from results limit 10) ;
ERROR 1064 (42000): 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 '1 from results where (select * from results l
imit 10)' at line 1
如何删除这些行?另外如何选择最后10行并删除它们?
答案 0 :(得分:2)
使用limit
时,您应始终拥有order by
。幸运的是,delete
接受limit
,因此:
delete r
from results r
order by id
limit 10;
SQL表没有"第一个"行,因为它们代表无序集。因此,如果您希望按特定顺序排列,则应使用ORDER BY
子句。
注意:可以使用LIMIT
而不ORDER BY
,但我不推荐它。
答案 1 :(得分:0)
你必须这样做,依赖于你将删除第一行或最后10行的ORDER:
DELETE FROM results ORDER BY id_filed DESC|ASC limit 10
答案 2 :(得分:0)
只需添加:
DELETE FROM `results` ORDER BY `row` DESC LIMIT 0,10;
希望这会对你有帮助(: