开发者

Update record with the biggest id value where nick = 'a'

开发者 https://www.devze.com 2023-02-09 15:58 出处:网络
What is the fastest way to update the record with the biggest id value where nick=\'a\' (in one query)?

What is the fastest way to update the record with the biggest id value where nick='a' (in one query)?

My table looks like this:

+------+--------+----+
| 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 |开发者_如何学Python
+------+--------+----+

I tried:

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

but The target table last_id of the UPDATE is not updatable Now I know it shouldn't work.

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

but You can't specify target table 'posts' for update in FROM clause


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

精彩评论

暂无评论...
验证码 换一张
取 消