惭愧,我的数据库太差劲了.... 一般就只会简单的select ,update 之类的
今天才知道,update 可以接 from 子句;update ... from .... ,以前一直是 update xxx set xxx=xxx where xxx
今天听了乔峰老师的讲解,才知道可以 update ... from ......
这个比较好理解。
最简单的方式,写一个 正确的 select 语句出来,把 前面的select 换成 update 就ok了。
------------------------------------------------------------------------------------------------------------------------
比如有:
表 A ; A 有两个字段 id,doFlag
表 B; B 有两个字段 aid,nType
我们要将表A中,id=表B的aid且 nType=0,的doFlag设为1。
我么可以先写这个select 语句,找出需要修改的记录集
select A.doFlag from A inner join B on A.id=B.aid where B.nType=0
然后把 select 语句改为 update 就OK
update A set A.doFlag=1 from A inner join B on A.id=B.aid where B.nType=0