發新話題

MySQL Replace 功能 [MySQL 內建函式]

MySQL Replace 功能 [MySQL 內建函式]

Mysql資料庫中字符替換,replace函數的使用
有時候遇到想替換資料庫裡某一字段的內容時可以用REPLACE函數:
for example:
UPDATE `supe_spacenews` SET `message` = REPLACE(`message`,'要替換的文字','替換後的文字') WHERE `message` LIKE '%要替換的文字%'

UPDATE `supe_spacenews` SET `message` = REPLACE(`message`,'http://www.dk101.com','http://bbs.ahpal.com');
上面是說,取代 message這個欄位的,找到 訪客無法瀏覽此圖片或連結,請先 註冊登入會員 取代成 訪客無法瀏覽此圖片或連結,請先 註冊登入會員
這語法相當好用,大家可以嘗試看看,畢竟如果用 php 的 replace 語法,還要利用陣列方式,比較麻煩。

語法如下:
update [table] set [field]=replace([field],’[str_search]’,'[str_replace]’);

UPDATE 表名 SET 字段名= REPLACE( 替換前的字段值, '替換前關鍵字', '替換後關鍵字' ) WHERE 字段名 REGEXP "替換前的字段值";

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,
'</td>',
'' );
UPDATE `dede_addonarticle` SET body = REPLACE ( body,
'</tr>',
'' );
UPDATE `dede_addonarticle` SET body = REPLACE ( body,
'<tr>',
'' );
UPDATE `dede_archives` SET title= REPLACE ( title,
'大洋新聞 - ',
'' );
UPDATE `dede_addonarticle` SET body = REPLACE ( body,
'../../../../../../',
'http://special.dayoo.com/meal/' );

mysql replace

用法1.replace intoreplace into table (id,name) values(『1『,『aa『),(『2『,『bb『)
此語句的作用是向表table中插入兩條記錄。
2.replace(object, search,replace)
把object中出現search的全部替換為replaceselect replace(『訪客無法瀏覽此圖片或連結,請先 註冊登入會員 『,『w『,『Ww『)--->WwW 訪客無法瀏覽此圖片或連結,請先 註冊登入會員

例:把表table中的name字段中的 aa替換為bbupdate table set name=replace(name,『aa『,『bb『)

=====================================================
MySQL Replace INTO說明
REPLACE的運行與INSERT很相像。只有一點除外,如果表中的一個舊記錄與一個用於PRIMARY KEY或一個UNIQUE索引的新記錄具有相同的值,則在新記錄被插入之前,舊記錄被刪除。請參見13.2.4節,「INSERT語法」。

注意,除非表有一個PRIMARY KEY或UNIQUE索引,否則,使用一個REPLACE語句沒有意義。該語句會與INSERT相同,因為沒有索引被用於確定是否新行複製了其它的行。

所有列的值均取自在REPLACE語句中被指定的值。所有缺失的列被設置為各自的默認值,這和INSERT一樣。您不能從當前行中引用值,也不能在新行中使用值。如果您使用一個例如「SET col_name = col_name + 1」的賦值,則對位於右側的列名稱的引用會被作為DEFAULT(col_name)處理。因此,該賦值相當於SET col_name = DEFAULT(col_name) + 1。

MySQL Replace INTO權限
為了能夠使用REPLACE,您必須同時擁有表的INSERT和DELETE權限。

REPLACE 語句會返回一個數,來指示受影響的行的數目。該數是被刪除和被插入的行數的和。如果對於一個單行REPLACE該數為1,則一行被插入,同時沒有行被刪除。如果該數大於1,則在新行被插入前,有一個或多個舊行被刪除。如果表包含多個唯一索引,並且新行複製了在不同的唯一索引中的不同舊行的值,則有可能是一個單一行替換了多個舊行。

受影響的行數可以容易地確定是否REPLACE只添加了一行,或者是否REPLACE也替換了其它行:檢查該數是否為1(添加)或更大(替換)。

如果您正在使用C API,則可以使用mysql_affected_rows()函數獲得受影響的行數。

目前,您不能在一個子查詢中,向一個表中更換,同時從同一個表中選擇。

以下是所用算法的更詳細的說明(該算法也用於LOAD DATA…REPLACE):

1. 嘗試把新行插入到表中
2. 當因為對於主鍵或唯一關鍵字出現重複關鍵字錯誤而造成插入失敗時:
a. 從表中刪除含有重複關鍵字值的衝突行
b. 再次嘗試把新行插入到表中

MySQL Replace INTO格式
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT},…),(…),…
或:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, …
或:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT …
=========================================
MySQL database has a handy and simple string function REPLACE() that allows table data with the matching string (from_string) to be replaced by new string (to_string). This is useful if there is need to search and replace a text string which affects many records or rows, such as change of company name, postcode, URL or spelling mistake.

The syntax of REPLACE is REPLACE(text_string, from_string, to_string)

MySQL reference describes REPLACE as function that returns the string text_string with all occurrences of the string from_string replaced by the string to_string, where matching is case-sensitive when searching for from_string. text_string can be retrieved from the a field in the database table too. Most SQL command can be REPLACE() function, especially SELECT and UPDATE manipulation statement.

For example:

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);

update client_table set company_name = replace(company_name, ‘Old Company’, ‘New Company’)

The above statement will replace all instances of ‘Old Company’ to ‘New Company’ in the field of company_name of client_table table.

Another example:

SELECT REPLACE(’訪客無法瀏覽此圖片或連結,請先 註冊登入會員 ’, ‘w’, ‘Ww’);

Above statement will return ‘WwWwWw.mysql.com’ as result.

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。