-1

テーブルを更新したい。

私のテーブル構造は次のとおりです。

  • MainTable:

    id | status | type | user
    
  • OtherTable:

    id | flag
    

ここで、設定されていない場所と場所を除くすべてのフィールドstatusとフィールドを更新したいのですが、どうすればよいですか?userMainTablestatus='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 に答える