【问题标题】:Update record with the biggest id value where nick = 'a'更新 id 值最大的记录,其中 nick = 'a'
【发布时间】:2011-06-25 02:35:07
【问题描述】:

最快的方法是什么 更新具有最大 id 值的记录,其中 nick='a'(在一个查询中)?

我的桌子是这样的:

+------+--------+----+
| nick | post   | id |
+------+--------+----+
| a    | tehe   |  1 |
| a    | tehe 2 |  2 |
| a    | tehe 3 |  3 |
| b    | omg    |  4 |
| b    | omg 2  |  5 |
| a    | tehe 4 |  6 |
| b    | omg 3  |  7 |
+------+--------+----+

我试过了:

update (select * from posts where nick='a' order by id limit 1) as last_id set post='tehe 4 updated';

但是UPDATE的目标表last_id是不可更新的 现在我知道它不应该起作用。

update posts set post = 'tehe 4? updated' where id = (select id from posts where nick='a' order by id desc limit 1); 

但您不能在 FROM 子句中指定目标表 'posts' 进行更新

【问题讨论】:

    标签: mysql mysql-error-1093


    【解决方案1】:
    update posts
        set post = 'tehe 4 updated'
        where nick='a'
        order by id desc limit 1
    

    【讨论】:

      猜你喜欢
      • 2019-07-05
      • 2013-12-17
      • 2017-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      相关资源
      最近更新 更多