更新具有最大id值的记录,其中nick ='a'

时间:2011-02-10 19:35:35

标签: mysql mysql-error-1093

最快的方式是什么? 更新具有最大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'

1 个答案:

答案 0 :(得分:1)

update posts
    set post = 'tehe 4 updated'
    where nick='a'
    order by id desc limit 1