关于update语句的问题

strivedl 2010-05-27 09:19:11
假如有一个表a。a中有id,pid,num三个属性,都是int类型的。

我想修改num的值
条件是,pid=2并且id最大

我这样写不对吗?update a set number=number+1 where id=(select max(id) from a where pid=2)
报错:you can't specify target table 're' for update in from clause
怎么写??
...全文
62 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhoulingsmile 2010-05-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 iihero 的回复:]
改成这种形式
mysql> update a12 set num=num+1 where id = (select max(id) from (select id from a12 t where pid=2) t);
Query OK, 1 row affected (0.06 sec)
[/Quote]
正解。。顶一个。。
jbvbrthnn 2010-05-27
  • 打赏
  • 举报
回复
顶一楼的
strivedl 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhoupuyue 的回复:]
SQL code

update a t1 left join a t2 on t1.id<t2.id
set t1.num=t1.num+1
where t1.pid=2 and t2.id is null;
[/Quote]

那left join ..on 呢
阿_布 2010-05-27
  • 打赏
  • 举报
回复
t1是什么啊?。。。
别名
strivedl 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhoupuyue 的回复:]
SQL code

update a t1 left join a t2 on t1.id<t2.id
set t1.num=t1.num+1
where t1.pid=2 and t2.id is null;
[/Quote]

t1是什么啊?。。。
strivedl 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 iihero 的回复:]
改成这种形式
mysql> update a12 set num=num+1 where id = (select max(id) from (select id from a12 t where pid=2) t);
Query OK, 1 row affected (0.06 sec)
[/Quote]

我的确是用的mysql,
这么麻烦吗?代码好长啊。。。全都是吗
阿_布 2010-05-27
  • 打赏
  • 举报
回复

update a t1 left join a t2 on t1.id<t2.id
set t1.num=t1.num+1
where t1.pid=2 and t2.id is null;
iihero_ 2010-05-27
  • 打赏
  • 举报
回复
改成这种形式
mysql> update a12 set num=num+1 where id = (select max(id) from (select id from a12 t where pid=2) t);
Query OK, 1 row affected (0.06 sec)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧