Update based on another table result

2019-09-05 21:41发布

I want to update a temp table based on another table result set.

Any suggestions. The select query works independently. But I am thinking to integrate with the update statement.

UPDATE #person_membership_promo_ext 
SET note_about=
(
    select note_text
    FROM note nt 
    INNER JOIN #person_membership_promo_ext per
      ON per.person_id=nt.main_ref_id
        and per.membership_type='P'
        and note_id=(select MAX(note_id)from note nt_1
    where nt_1.main_ref_id=per.person_id)
) 

3条回答
霸刀☆藐视天下
2楼-- · 2019-09-05 21:52

You can use joins in update statements

UPDATE per
SET note_about = nt.note_text
FROM #person_membership_promo_ext per
INNER JOIN note nt
  ON per.person_id=nt.main_ref_id
    and per.membership_type='P'
    and note_id = (
        select MAX(note_id) 
        from note nt_1
        where nt_1.main_ref_id = per.person_id
   )
查看更多
疯言疯语
3楼-- · 2019-09-05 21:56

Here you go,

Update per
set per.note_about = nt.note_text
FROM note nt 
INNER JOIN #person_membership_promo_ext per
ON per.person_id=nt.main_ref_id
and per.membership_type='P'
and note_id=(select MAX(note_id)from note nt_1
where nt_1.main_ref_id=per.person_id)

I hope this will help!

查看更多
三岁会撩人
4楼-- · 2019-09-05 22:08
UPDATE per
SET note_about=nt.note_text
FROM note nt 
INNER JOIN #person_membership_promo_ext per
  ON per.person_id=nt.main_ref_id
    and per.membership_type='P'
    and note_id=(select MAX(note_id)from note nt_1
where nt_1.main_ref_id=per.person_id)
查看更多
登录 后发表回答