Rocho.J

人脑是不可靠的, 随时记录感悟并且经常重复!

 

update语句更新多条记录, 标记下

转 ---  的两篇内容

View Code
update多条Select出来的数据

好多朋友喜欢用游标解决此问题,但是执行速度狂慢!
其实解决起来很简单了:

先来个简单的:
把FLowER的Am_employee表的email,dept_id,ext_no多条数据按emp_no对应update到EmpBaseInfo表中:
update EmpBaseInfo
set email=b.Mail_account,dept_id=b.dept_code,ext_no=b.ext_no
from flower.flower.dbo.am_employee b
where b.emp_no=EmpBaseInfo.emp_no

复杂一点的:
多表select,并将结果对应插入到指定的表中:

update EmpBaseInfo
set Job_Rank=b.level_number
from flower.flower.dbo.am_employee a,flower.flower.dbo.am_level b,flower.flower.dbo.am_approver_level c
where a.emp_id=c.emp_id and b.level_number=c.level_number and a.emp_no=EmpBaseInfo.emp_no


取别名的:
update x
set Job_Rank=b.level_number
from flower.flower.dbo.am_employee a,flower.flower.dbo.am_level b,flower.flower.dbo.am_approver_level c,EmpBaseInfo x
where a.emp_id=c.emp_id and b.level_number=c.level_number and a.emp_no=x.emp_no

 

--另外一种方式: 临时表

View Code
偶用临时表来解决
1、创建临时表#temp_1
SELECT card_id,sum(card_cost) as 'sum_cost'
INTO #tmp_1
FROM pos_t_vip_cost
GROUP by card_id
go
2、更新表pos_t_vip_info记录
UPDATE pos_t_vip_info,#tmp_1
SET pos_t_vip_info.card_amount = #tmp_1.sum_cost
WHERE pos_t_vip_info.card_id = #tmp_1.card_id
go
3、删除临时表
DROP TABLE #tmp_1
go

 

以上是参考资料

 

View Code
update [StormPlatform120316].[dbo].[t_Assessment]
set Developers = le.DevelopmentPrincipal
from [StormPlatform120316].[dbo].[t_Assessment] a left join [StormPlatform0623].[dbo].[t_LeaderEvaluation] le on a.RequirementApplyID = le.RequirementApplyID and le.createtime in (select max(createtime) from [StormPlatform0623].[dbo].[t_LeaderEvaluation] group by RequirementApplyID)
where a.Developers = ''



Mysql的update语句

View Code
2010-04-24 20:31 mysql update from 子查询mssql 子查询更新

update log set uin= b.uin
from log a,logs b
where a.accountuin = b.accountuin

mysql 不支持 update 子查询更新

找了半天资料 终于搞定了...

update `log` a inner join `logs` b on a.`accountuin` = b.`accountuin`
set a.`uin` = b.`uin` where a.`accountuin` = b.`accountuin`

这样就能 将查询出来的字段批量更新过去了...

 

//delete的联合查询

Update 


Update XXX set XXX where 这种写法大家肯定都知道,才发现update和delete居然支持inner join的update方式,这个在表间关联来做更新和删除操作非常有用. 

列子: 




Sql代码 
update tb_User   
set pass=''  
from tb_User usr   
inner join tb_Address addr on usr.nAddressFK = addr.nAddressID   
where usr.id=123  

update tb_User 
set pass='' 
from tb_User usr 
inner join tb_Address addr on usr.nAddressFK = addr.nAddressID 
where usr.id=123update的格式是 

update t1 set t1.name=’Liu’ from t1 inner join t2 on t1.id = t2.tid 




MYSQL,ACCESS 写法如下: 




Sql代码 
UPDATE mem_world AS mw1 INNER JOIN mem_world  AS  mw2     
ON  mw1.parentid = mw2.wid    
SET mw1.level = mw2.level     
WHERE mw2.baseid = 107     
AND  mw2.parentid = 0     
AND  mw2.size > 1;  

UPDATE mem_world AS mw1 INNER JOIN mem_world  AS  mw2  
ON  mw1.parentid = mw2.wid 
SET mw1.level = mw2.level  
WHERE mw2.baseid = 107  
AND  mw2.parentid = 0  
AND  mw2.size > 1; 

on是表连接的筛选条件 
就是说,表连接后,会产生一个类似于临时的视图这么一个东西 
where是从这个临时的视图中筛选数据的 
所以,你首先要搞清,你的所谓的2个条件属于哪一种 




Delete 




delete 语句也是类似 

delete from t1 from t1 inner join t2 on t1.id = t2.tid 

注意蓝色部分。 

mysql: 


Sql代码 
DELETE mwb FROM  mem_world_building AS mwb INNER JOIN mem_world AS mw   
ON mwb.wid = mw.wid   
where mw.type between 11 and 15    
and baseid = 107    
and mw.parentid <> 0    
and  mw.size > 1;  

DELETE mwb FROM  mem_world_building AS mwb INNER JOIN mem_world AS mw 
ON mwb.wid = mw.wid 
where mw.type between 11 and 15 
and baseid = 107 
and mw.parentid <> 0 
and  mw.size > 1;  
下面是ORACLE的: 


Sql代码 
DELETE TABLE1 where exists ( select 1 from table2 where and table1.khid=table2.khid and FWDWID=8);  

DELETE TABLE1 where exists ( select 1 from table2 where and table1.khid=table2.khid and FWDWID=8); 



Sql代码 
DELETE TABLE1 where KHID exists ( select KHID from table2 where FWDWID=8)  

DELETE TABLE1 where KHID exists ( select KHID from table2 where FWDWID=8)
View Code

 

 

posted on 2012-03-20 22:52  RJ  阅读(...)  评论(... 编辑 收藏

导航

统计