-1

我想更新一个表。

我的表结构是:

  • MainTable

    id | status | type | user
    
  • OtherTable

    id | flag
    

在这里,我想更新所有status和不包括 where和 where on not setuser的字段ie ,但是如何?MainTablestatus='Stop'flagOtherTable0

OtherTable仅持有价值type='EMP'

更新:我想在MainTable何时使用条件检查更新所有记录type ='EMP'

4

3 回答 3

1
Update MT set user='someuser', status = "somestatus" 
FROM MainTable MT
Join
(
Select * 
from MainTable MT1
Join Othertable OT on MT1.Id = OT.Id
AND OT.Flag = 0
and  MT1.type='EMP'
Union All
Select * 
from MainTable MT2
WHERE MT2.id not in (Select ID FROM Othertable where Flag = 0)
and  MT2.type='EMP'
) tblOther
on MT.ID = tblOther.ID
于 2012-06-26T06:59:44.613 回答
1

@Asif 给出了智能答案

这是适合我要求的更简单/替代方式


 `update MainTable
  set status='someStatus'
  where 
  status!='Stop'
  and check_type!='EMP'

  update MainTable
  set status='someStatus'
  from MainTable inner join OtherTable on OtherTable.id=MainTable.id
  where 
  OtherTable.flag=1` 
于 2012-06-26T07:40:53.223 回答
0

使用子查询....

Update MainTable Set user="" where status='STOP' and id in (select id from OtherTable 
where Flag!=0)
于 2012-06-26T05:37:48.070 回答