从SQL Server中的2个不同表更新ID

时间:2015-10-12 20:53:20

标签: sql-server

我有3张桌子:

  • t1:id,m_id,date
  • t2:p_id,p_code
  • t3:p_id,p_code,event_id

如何更新t1中的ID?

where id in (select p_id from t2 inner join t3 on t2.p_code = t3.p_code)

1 个答案:

答案 0 :(得分:0)

UPDATE t1
SET id = (SELECT TOP 1 t2.p_id
            FROM t2 INNER JOIN t3
            WHERE t2.p_code = t3.p_code)
WHERE <conditon> -- choose rows to update