Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UPDATE last 10 records of PHP Mysql table

Tags:

php

mysql

It seems a simple question but unfortunately I can't find examples.

Suppose that I want use the UPDATE sentence into php for updating the last 10 records of my table "users" of Database "users". what its the code for that? I mean INSERT sentence have a
answer for that using LIMIT etc, but UPDATE dont have it.

Thanks in advance for your help.

pd: For example, I want to update "Firstname" field with "michael" word in the last 10 records.

like image 768
JuanFernandoz Avatar asked Jan 08 '13 01:01

JuanFernandoz


1 Answers

Try this

UPDATE table SET notes="hi"
ORDER BY id DESC
LIMIT 10

As per Mysql docs, if there is a unique column, order by it DESC, and use LIMIT 10 to select last 10 records.

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
        SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
        [WHERE where_condition]
        [ORDER BY ...]
        [LIMIT row_count]
like image 140
rizon Avatar answered Oct 03 '22 01:10

rizon