更新具有最大 id 值的记录,其中 nick = 'a'
最快的方法是什么 更新具有最大 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”
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 |
+------+--------+----+
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)