Mysql如何对同一个表进行update(insert)和select操作

csiamgo1 2011-01-06 10:16:04
如图,具体语句如下,大家帮忙改下,原来的是SQL Server的语句,现在要改成Mysql的
update Type set Type_Name='Communication', Type_Desc='Phase: Requirement', Parent_Id=(select ID from Type where Type_Name='Requirement' and Parent_Id=0), Order_Number=1 where ID=1;

insert into Type 
select 'Review','Phase: Requirement',ID,3 from Type where Type_Name='Requirement' and Parent_Id=0;
...全文
382 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
老骥伏枥 2011-01-10
  • 打赏
  • 举报
回复
来学习的,看来就是用mysql的自连接来解决。
wwwwb 2011-01-06
  • 打赏
  • 举报
回复
就是自连接查询
update Type a,type b
set a.Type_Name='Communication', a.Type_Desc='Phase: Requirement', a.Parent_Id=b.id, a.Order_Number=1 where a.ID=1 and b.Type_Name='Requirement' and b.Parent_Id=0;
小小小小周 2011-01-06
  • 打赏
  • 举报
回复
csiamgo1 2011-01-06
  • 打赏
  • 举报
回复
已经自己解决了,附上代码
update Type a,(select ID from Type where Type_Name='Requirement' and Parent_Id=0) b set a.Type_Name='Communication', a.Type_Desc='Phase: Requirement', a.Parent_Id=b.id, a.Order_Number=1 where a.ID=1;

insert into Type(Type_Name,Type_Desc,Parent_Id,Order_Number) 
select 'Review','Phase: Requirement',ID,3 from Type where Type_Name='Requirement' and Parent_Id=0;
小小小小周 2011-01-06
  • 打赏
  • 举报
回复
你就当是2张表,自连接来更新
csiamgo1 2011-01-06
  • 打赏
  • 举报
回复
You can't specify target table 'Type' for update in FROM clause
网上说是不能在update的时候在同一个表做select操作
wwwwb 2011-01-06
  • 打赏
  • 举报
回复
SQL语句有问题?提示什么
ACMAIN_CHM 2011-01-06
  • 打赏
  • 举报
回复
MYSQL这个比较特殊,一般是通过 表的自连接来实现,也就是你使用的这种方法。

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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