
取代MySQL 中的多個字元
當面對修改MySQL 欄位中的多個字元的任務時,REPLACE 函數會出現不足,因為它是為單字元替換而設計的。
可以做到嗎快點嗎?
是的! MySQL 提供了一個解決方案:連結 REPLACE 函數。
1 | select replace(replace( 'hello world' , 'world' , 'earth' ), 'hello' , 'hi' )
|
登入後複製
此指令將“hello”替換為“hi”,將“world”替換為“earth”,結果是“hi Earth”。
多重替換的高級技術
對於更複雜的替換、子查詢和可以使用JOIN:
子查詢:
1 2 3 4 | select replace(london_english, 'hello' , 'hi' ) as warwickshire_english
from (
select replace( 'hello world' , 'world' , 'earth' ) as london_english
) sub
|
登入後複製
此子查詢將「london_english」中的「hello」替換為「hi」
JOIN:
1 2 3 4 5 6 7 8 9 10 11 | select group_concat(newword separator ' ' )
from (
select 'hello' as oldword
union all
select 'world'
) orig
inner join (
select 'hello' as oldword, 'hi' as newword
union all
select 'world' , 'earth'
) trans on orig.oldword = trans.oldword
|
登入後複製
此JOIN 組合兩個表格以取代單一查詢中的多個字元。
以上是如何快速替換MySQL字段中的多個字元?的詳細內容。更多資訊請關注PHP中文網其他相關文章!