K. Zakrzewski
K. Zakrzewski

Reputation: 59

How to update one value in the last row in 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)

Upvotes: 2

Views: 2115

Answers (1)

Shadow
Shadow

Reputation: 34285

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;

Upvotes: 3

Related Questions