MySQL的String函數locate和replace實戰

來源:互聯網
上載者:User

有關mysql中的locate函數可以查看該連結:http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_locate

有關mysql中的replace函數可以查看該連結: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

以上便是今天我們要介紹使用的兩個函數。

使用背景:

條件:field_data_body 表的body_value值中有這樣一個規律的字串"/eapdomain/UserFiles/1073/File/"、"/eapdomain/UserFiles/1073/File/",其中1073是使用者id。不要驚訝為什麼會有這樣的路徑!

目的:body_value中所有的符合上述規律的字串替換為 "/sites/default/files/old/image/","/sites/default/files/old/file/"。

痛點:資料量略大4千條資料,不可能手動更改;每條資料中可能存在著不止一個這樣的路徑或者不存在;使用者id為變動的,不能確定。

直接操作資料庫吧!

在Navicat中,開啟你的資料庫命令列工具(不限於此工具),輸入以下的命令進行替換工作。

替換包含image的字串
   
UPDATE field_data_body set body_value = replace(body_value, substring(body_value, locate('/eapdomain/UserFiles/', body_value),locate('/Image/', body_value)+7-locate('/eapdomain/UserFiles/', body_value)),'/sites/default/files/old/image/')

替換包含file的字串
   
UPDATE field_data_body set body_value = replace(body_value, substring(body_value, locate('/eapdomain/UserFiles/', body_value),locate('/File/', body_value)+6-locate('/eapdomain/UserFiles/', body_value)),'/sites/default/files/old/file/')

能夠實現該目的的方法會很多,歡迎大家在下邊留言。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.