MySQL update語句 批量更新某欄位

2021-10-07 00:27:11 字數 1261 閱讀 4203

mysql:

1 有時候要實現欄位的批量替換

update 表名 set 欄位a = replace(欄位a ,"png","jpg" );

2 批量拼接字串到某欄位

update 表名 set 欄位a = concat( 欄位a  , "***"  );

update 表名 set 欄位a = concat( "yyy" , 欄位a  , "***"  );

3 批量擷取某欄位,扔掉開始的4個字元

update 表名 set 欄位a=substr(欄位a,4);

4 批量擷取某欄位,保留結尾的3個字元

update 表名 set 欄位a=substr(欄位a,-3);

5 批量擷取某欄位,去掉結尾的2個字元

update 表名 set 欄位a=substr(欄位a,1,length(欄位a)-2);

更詳細的方法請參考mysql的substr函式

------------update+select----------------------------------以下是網際網路收集,用的時候再仔細驗證

/*多表關聯update的時候,記得要加exists()條件,否則不滿足條件的記錄被update稱null:

比如:stu表存在,但stu1表不存在的資料,對應的字段會被updat成null;

*/6 多表關聯update單字段

update stu t set t.name = (select t1.name from stu1 t1 where t1.id = t.id)

where exists(select 1 from stu1 t2 where t2.id = t.id);

7 多表關聯update多欄位

update stu t set (t.name, t.***) = (select t1.name, t1.*** from stu1 t1 where t1.id = t.id)

where exists(select 1 from stu1 t2 where t2.id = t.id);

update table1 alias

set (column_name,column_name ) = (

select (column_name, column_name)

from table2

where column_name = alias.column_name)

where column_name = value

mysql:

MySQL update語句流程總結

廢話不多說先來張 釋 update t set value value 1 where id 2 複製 我想可能大部分人看完這圖,思考片刻,接下來的就不需要在繼續看了,但是考慮到部分朋友還是新手 包括自己 以及後面複習,還是稍微嘮叨一段。首先,上圖中深色背景的表示在執行器中執行,也就是server層...

Mysql Update語句的詳細用法

以下的文章主要介紹的是mysql update 語句的實際用法,我們首先是以單錶的update語句來引出實現mysql update 語句的實際方案,以下就是文章的詳細內容描述,望你看完之後會有收穫。單錶的mysql update語句 update low priority ignore tbl n...

mysql update語句與limit的結合使用

mysql的update語句只支援更新前多少行,不支援從某行到另一行,比如 update tb name set column name test order by id asc limit 30 更新前30行的某個字段內容,沒什麼問題。update tb name set column name ...