douhuan6305 2016-08-18 06:37
浏览 33
已采纳

如何在php的最后一行更新一个值

I just wanted to know how should look my sql query to make an update of one value in the last row of my table. I tried to do this by query which is unfortunately not working:

UPDATE tableName SET food=888 WHERE id=(SELECT MAX(id) FROM tableName)

I tried to use this query instead but it's incorrect aswell:

UPDATE tableName SET food=88 ORDER BY id DESC LIMIT 1

(I missed quotation marks in queries on purpose to make them clearly)

  • 写回答

1条回答 默认 最新

  • doufu7835 2016-08-18 06:54
    关注

    Your 2nd query works, I tested it in sqlfiddle

    CREATE TABLE tableName
        (`Id` int, `food` int)
    ;
    
    INSERT INTO tableName
        (`Id`, `food`)
    VALUES
        (1, 8)
    ;
    
    UPDATE tableName SET food=88 ORDER BY id DESC LIMIT 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?