關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • MySQL 的 Replace 功能

    Posted on February 15th, 2006 Sam Tang 2 comments

    要對 MySQL 資料庫內的資料替換字串,可以借助 PHP 的 str_replace(),但這實在太麻煩了,其實MySQL 已內建了這個功能,以下語句可以將 [table] 資料表的 [field] 欄位作 replace 動作,[str_search] 是搜索字串;[str_replace] 是替換字串。

    update [table] set [field]=replace([field],’[str_search]‘,’[str_replace]‘);

    如果不想將整個資料表的欄位資料更換,可以在最後用 where 做條件選擇,例如:

    update [table] set [field]=replace([field],’[str_search]‘,’[str_replace]‘) where `zip`=’852′;

    以上語句跟第一句的分別是,第二句只會對欄位 “zip” 為 “852″ 的紀錄作更改。


     

    2 responses to “MySQL 的 Replace 功能”

    1. a = TR_Rec.Fields(“THSRC”)
      b = TR_Rec.Field(“TSC”)MySQL_Conn.Execute “update route set route = replace(route, ‘a’, ‘b’)

      please advise how to use a and b as variabls. 3q!

    2. 滿不錯的用法

      By SIKO

    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word