MySQL資料庫update更新子查詢

2021-12-30 11:40:59 字數 1550 閱讀 1327

mysql資料庫update更新子查詢。

比如:update test.tb_vobile a set a.name = '111 ' where a.id = (select max(id) id from test.tb_vobile)

報錯:[sql]update test.tb_vobile a set a.name = '111 ' where a.id = (select max(id) id from test.tb_vobile)

以下可通過:

update test.tb_vobile a join (select max(id) id from test.tb_vobile) b on a.id = b.id set a.name = '123 '; 或 update test.tb_vobile a ,(select max(id) id from test.tb_vobile) b set a.name = '321 ' where a.id = b.id ;

說明:1、update 時,更新的表不能在set和where中用於子查詢;

2、update 時,可以對多個表進行更新(sqlserver不行);

如:update ta a,tb b set a.bid=b.id ,b.aid=a.id;

3、update 後面可以做任意的查詢,這個作用等同於from;

update test.tb_vobile a

set a.name = '111 '

where

a.id = (select max(id) id from test.tb_vobile)

報錯:[sql]update test.tb_vobile a set a.name = '111 ' where a.id = (select max(id) id from test.tb_vobile) [err] 1093 - you can't specify target table 'a' for update in from clause

以下可通過:

update test.tb_vobile a join (select max(id) id from test.tb_vobile) b on a.id = b.id set a.name = '123 '; 或 update test.tb_vobile a ,(select max(id) id from test.tb_vobile) b set a.name = '321 ' where a.id = b.id ;

update user_tb a,(select userid from user_tb where userid>1 order by userid limit 0,10) b

set a.nickname='哈哈哈' where a.userid in (b.userid);

說明:1、update 時,更新的表不能在set和where中用於子查詢;

2、update 時,可以對多個表進行更新(sqlserver不行);

如:update ta a,tb b set a.bid=b.id ,b.aid=a.id;

3、update 後面可以做任意的查詢,這個作用等同於from;

資料庫update死鎖

比較常見的死鎖場景,併發批量update時的乙個場景 update cross marketing set gmtmodified now pageview pageview extpageview where marketingid marketingid 第一次呼叫時,marketingid傳入...

MySQL資料庫update更新子查詢

比如 12 3 4 update test.tb vobile a set a.name 111 where a.id select max id id from test.tb vobile 報錯 1 2 3 4 5 6 7 sql update test.tb vobile a set a.na...

一次mysql資料庫從庫UPDATE失敗的分析

庫 mysql5.6.19 從 mysql5.6.37 場景 昨天開發組反應從庫和主庫的資料同步有問題,在主庫中進行更新過的資料,從庫中有的沒有更新,導致他們根據觸發器變化的資料不準確。起先接到這個問題,我把驚著了,按理說mysql從庫的版本遠遠高於主庫的,即使要出問題,也應該是早期的從庫版本出問題...