MySQL 使用sql查询删除表中的最后一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32502830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
delete the last row in a table using sql query?
提问by Arunkumar G
I am trying to delete the last record in the table named "marks"in the database using MySql query. I was tried to do so. Also I tried on the localhost. The query I tried for this is as follows:
我正在尝试使用MySql query删除数据库中名为“marks”的表中的最后一条记录。我试图这样做。我也试过localhost。我为此尝试的查询如下:
DELETE MAX(`id`) FROM `marks`;
There are 8 columns in the table. I want to delete the last column without specifyingDELETE FROM marks where id=8
; After deleting the 8th record, i want to delete the 7th after that 6th and so on upto 1st record without specifying manually.
表中有 8 列。我想删除最后一列而不指定DELETE FROM marks where id=8
;删除第 8 条记录后,我想删除第 6 条之后的第 7 条记录,直到第 1 条记录,而无需手动指定。
Please help me in this issue with example coding. Thanks in advance.
请通过示例编码帮助我解决这个问题。提前致谢。
回答by Abhik Chakraborty
If id
is auto-increment then you can use the following
如果id
是自动增量,那么您可以使用以下内容
delete from marks
order by id desc limit 1
回答by Chintan7027
@Abhshek's Answer is right and works for you but you can also try the following code if the id is not increment
@Abhshek 的答案是正确的并且适合您,但如果 id 不递增,您也可以尝试以下代码
DELETE FROM MARKS WHERE ID=(SELECT MAX(id) FROM MARKS)
回答by Sanoj Dushmantha
This is not the best but a different solution.
这不是最好的,而是不同的解决方案。
DELETE FROM marks
WHERE id = (SELECT id
FROM marks
ORDER BY id DESC
LIMIT 1);
回答by Jose Pinto
Try this:
尝试这个:
$check = $db->prepare("SELECT `file` FROM `marks` WHERE `id` = ?");
$check->execute(array($id));
while ($checks=$check->fetchObject()) {
$unlink = unlink($uploadDir.$checks->file);
}
Where:
在哪里:
uploadDiris your directory;
$checks->fileis your file name, saved in database;
$idis the ID row you want to delete the file;
uploadDir是你的目录;
$checks->file是你的文件名,保存在数据库中;
$id是要删除文件的ID行;