mySQL中replace的用法

2021-06-26 04:42:52 字數 3349 閱讀 7922

mysql replace函式我們經常用到,下面就為您詳細介紹mysql replace函式的用法,希望對您學習mysql replace函式方面能有所啟迪

mysql replace例項說明:

update tb1 set f1=replace(f1, 'abc', 'def'); 

replace(str,from_str,to_str) 

在字串 str 中所有出現的字串 from_str 均被 to_str替換,然後返回這個字串 

這個函式用來批量替換資料中的非法關鍵字是很有用的!如下例子: 

例1:update bbstopic set tcontents = replace(replace(tcontents,'共產黨','') ,'找死','') where tcontents like '%共產黨%' or tcontents like '%找死%' 

例2:update typetable set type_description=replace(type_description,'360',''); 

mysql replace用法

1.replace into 

replace into table (id,name) values('1','aa'),('2','bb') 

此語句的作用是向表table中插入兩條記錄。如果主鍵id為1或2不存在 

就相當於 

insert into table (id,name) values('1','aa'),('2','bb') 

如果存在相同的值則不會插入資料 

2.replace(object,search,replace) 

把object中出現search的全部替換為replace 

select replace('www.jb51.net','w','ww')--->wwwwww.jb51.net 

例:把錶table中的name欄位中的aa替換為bb 

update table set name=replace(name,'aa','bb') 

mysql replace函式我們經常用到,下面就為您詳細介紹mysql replace函式的用法,希望對您學習mysql replace函式方面能有所啟迪。 

最近在研究cms,在資料轉換的時候需要用到mysql的mysql replace函式,這裡簡單介紹一下。 

比如你要將表 tb1裡面的 f1欄位的abc替換為def 

update tb1 set f1=replace(f1, 'abc', 'def'); 

replace(str,from_str,to_str) 

在字串 str 中所有出現的字串 from_str 均被 to_str替換,然後返回這個字串: 

mysql> select replace('www.mysql.com', 'w', 'ww'); 

-> 'wwwwww.mysql.com' 

這個函式是多位元組安全的。 

示例: 

update `dede_addonarticle` set body = replace ( body, 

'', 

'' ); 

update `dede_addonarticle` set body = replace ( body, 

'', 

'' ); 

update `dede_addonarticle` set body = replace ( body, 

'', 

'' ); 

update `dede_archives` set title= replace ( title, 

'大洋新聞 - ', 

'' ); 

update `dede_addonarticle` set body = replace ( body, 

'../../../../../../', 

'' ); 

mysql replace 

用法1.replace intoreplace into table (id,name) values(『1『,『aa『),(『2『,『bb『) 

此語句的作用是向表table中插入兩條記錄。 

2.replace(object, search,replace) 

把object中出現search的全部替換為replaceselect replace(『www.jb51.net『,『w『,『ww『)--->www www.jb51.net 

例:把錶table中的name欄位中的 aa替換為bbupdate table set name=replace(name,『aa『,『bb『)

在向表中插入資料的時候,經常遇到這樣的情況:1. 首先判斷資料是否存在; 2. 如果不存在,則插入;3.如果存在,則更新。

在 sql server 中可以這樣處理:

if not exists (select 1 from t where id = 1)

insert into t(id, update_time) values(1, getdate())

else

update t set update_time = getdate() where id = 1

那麼 mysql 中如何實現這樣的邏輯呢?別著急!mysql 中有更簡單的方法:replace into

replace into t(id, update_time) values(1, now());

replace into t(id, update_time) select 1, now();
replace into跟 insert 功能類似,不同點在於:replace into首先嘗試插入資料到表中, 1. 如果發現表中已經有此行資料(根據主鍵或者唯一索引判斷)則先刪除此行資料,然後插入新的資料。 2. 否則,直接插入新資料。

要注意的是:插入資料的表必須有主鍵或者是唯一索引!否則的話,replace into會直接插入資料,這將導致表中出現重複的資料。

1. replace into tbl_name(col_name, ...) values(...)

2. replace into tbl_name(col_name, ...) select ...

3. replace into tbl_name set col_name=value, ...

前兩種形式用的多些。其中 「into」 關鍵字可以省略,不過最好加上 「into」,這樣意思更加直觀。另外,對於那些沒有給予值的列,mysql 將自動為這些列賦上預設值。

mySQL中replace的用法

mysql replace例項說明 update tb1 set f1 replace f1,abc def replace str,from str,to str 在字串 str 中所有出現的字串 from str 均被 to str替換,然後返回這個字串 這個函式用來批量替換資料中的非法關鍵字是...

mySQL中replace的用法

mysql replace函式我們經常用到,下面就為您詳細介紹mysql replace函式的用法,希望對您學習mysql replace函式方面能有所啟迪update tb1 set f1 replace f1,abc def replace str,from str,to str 在字串 str...

mySQL中replace的用法

mysql有什麼辦法批量去掉某個字段字元中的空格?不僅是字串前後的空格,還包含字串中間的空格,答案是 replace,使用mysql自帶的 replace 函式,另外還有個 trim 函式。1 mysql replace 函式 語法 replace object,search,replace 意思 ...