對單表執行更新沒有什麼好說的,無非就是update table_name set col1 = xx,col2 = yy where col = zz,主要就是where條件的設置。有時候更新某個表可能會涉及到多張數據表,例如:
代碼如下 復制代碼update table_1 set score = score + 5 where uid in (select uid from table_2 where sid = 10);
其實update也可以用到left join、inner join來進行關聯,可能執行效率更高,把上面的sql替換成join的方式如下:
update table_1 t1 inner join table_2 t2 on t1.uid = t2.uid set score = score + 5 where t2.sid = 10;
mysql關聯多表進行update更新操作
UPDATE Track
INNER JOIN MV
ON Track.trkid=MV.mvid
SET Track.is_show=MV.is_show
WHERE trkid<6
等同於
UPDATE Track,MV
SET Track.is_show=MV.is_show
WHERE Track.trkid=MV.mvid and trkid<6