delete the last row in a table using sql query?

前端 未结 4 1712
[愿得一人]
[愿得一人] 2021-01-02 02:43

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

相关标签:
4条回答
  • 2021-01-02 02:50

    @Abhshek's Answer is right and works for you but you can also try the following code if the id is not increment

    DELETE FROM MARKS WHERE ID=(SELECT MAX(id) FROM MARKS)
    
    0 讨论(0)
  • 2021-01-02 02:57

    This is not the best but a different solution.

    DELETE FROM marks 
    WHERE  id = (SELECT id 
                 FROM   marks 
                 ORDER  BY id DESC 
                 LIMIT  1); 
    
    0 讨论(0)
  • 2021-01-02 03:05

    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:

    • uploadDir is your directory;

    • $checks->file is your file name, saved in database;

    • $id is the ID row you want to delete the file;

    0 讨论(0)
  • 2021-01-02 03:13

    If id is auto-increment then you can use the following

    delete from marks
    order by id desc limit 1
    
    0 讨论(0)
提交回复
热议问题