简体   繁体   English

更新PHP Mysql表的最后10条记录

[英]UPDATE last 10 records of PHP Mysql table

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". 假设我想在php中使用UPDATE语句来更新数据库“users”的表“users”的最后10条记录。 what its the code for that? 它的代码是什么? I mean INSERT sentence have a 我的意思是INSERT句子有一个
answer for that using LIMIT etc, but UPDATE dont have it. 使用LIMIT等答案,但UPDATE没有。

Thanks in advance for your help. 在此先感谢您的帮助。

pd: For example, I want to update "Firstname" field with "michael" word in the last 10 records. pd:例如,我想在最后10条记录中用“michael”字更新“Firstname”字段。

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. 根据Mysql文档,如果有一个唯一的列,请按DESC排序,并使用LIMIT 10选择最后10条记录。

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
        SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
        [WHERE where_condition]
        [ORDER BY ...]
        [LIMIT row_count]

You can do something like this. 你可以做这样的事情。

Method 1 方法1

UPDATE table_name SET column_name='value' WHERE id IN (SELECT id FROM table_name ORDER BY id desc LIMIT 0, 10);

Method 2 方法2

If you look at the docs 如果你看看文档

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

Which means below also should work for you 这意味着下面也适合你

UPDATE table_name SET column_name='value' ORDER BY id desc LIMIT 10;

Try one of them which suits you and let me know if you have any issues. 尝试其中一个适合您的,如果您有任何问题,请告诉我。

您没有向表模式提供任何输入,但这里有一些可行的方法:

UPDATE users SET firstname='michael' WHERE id >= (SELECT MAX(id)-10 FROM users);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM