SQL中的替換函數(shù)replace總結(jié)

2023-08-05    分類: 網(wǎng)站建設(shè)

創(chuàng)新互聯(lián)建站介紹我們在程序或數(shù)據(jù)庫開發(fā)中往往會(huì)用到的sql語句替換函數(shù)replace()

替換函數(shù)replace()有三種替換方式:1、查詢替換;2、更新替換;3新增替換。接下來創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司來舉幾個(gè)例子一看便知其使用!

SQL中的替換函數(shù)replace語法
REPLACE ( string_expression , string_pattern , string_replacement )

replace參數(shù)說明
string_expression 需要替換的字段(可以是字符或二進(jìn)制數(shù)據(jù)類型)
string_pattern 查詢的子字符串。(可以是字符或二進(jìn)制數(shù)據(jù)類型。string_pattern 不能是空字符串 ''
string_replacement 替換的字符串。(可以是字符或二進(jìn)制數(shù)據(jù)類型)

1、replace查詢替換:
原數(shù)據(jù):
id  title     content     times
1   網(wǎng)占建設(shè)  創(chuàng)新互聯(lián)...  2023.6.23
這里我要替換字段title里面的占,替換為站存到字段new_title中,replace的sql語句:
SELECT *,REPLACE(title,'占','站') as new_title FROM cdcxhl_news LIMIT 1
執(zhí)行的上現(xiàn)的replace的sql語句得到新數(shù)據(jù):
id  title          content       times         new_title
1   網(wǎng)占建設(shè)  創(chuàng)新互聯(lián)...  2023.6.23   網(wǎng)站建設(shè)


2、replace更新替換:
原數(shù)據(jù):
id  title     content     times
1   網(wǎng)占建設(shè)  創(chuàng)新互聯(lián)...  2023.6.23
這里我要替換字段title里面的占,替換為站,replace的sql語句:
UPDATE cdcxhl_news SET title = REPLACE(title,'占','站') WHERE id = 1
如果你的數(shù)據(jù)不只一條有多條,把字段title里面的網(wǎng)占建設(shè),替換為網(wǎng)站建設(shè),replace的sql語句:
UPDATE cdcxhl_news SET title = REPLACE(title,'網(wǎng)占建設(shè)','網(wǎng)站建設(shè)') 

執(zhí)行的上現(xiàn)的replace的sql語句得到新數(shù)據(jù):
id  title         content        times         new_title
1   網(wǎng)占建設(shè)  創(chuàng)新互聯(lián)...  2023.6.23   網(wǎng)站建設(shè)


3、replace新增替換:
原數(shù)據(jù):
id  title     content     times
1   網(wǎng)站制作  創(chuàng)新互聯(lián)...  2023.6.23
這里我將字段title,content分別替換為網(wǎng)站推廣,快上推...,replace的sql語句:
REPLACE INTO cdcxhl_news (id,title,content) VALUES (1,'網(wǎng)站推廣','快上推...')
執(zhí)行的上現(xiàn)的replace的sql語句得到新數(shù)據(jù):
id  title           content     times
1   網(wǎng)站推廣   快上推...   2023.6.23

注意:新增替換是根據(jù)唯一主鍵來判斷的,如果替換數(shù)據(jù)的唯一主鍵id和需要替換的唯一主鍵值相同時(shí)為更新替換,不同時(shí),為新增

網(wǎng)頁標(biāo)題:SQL中的替換函數(shù)replace總結(jié)
文章URL:http://www.muchs.cn/news25/274825.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、靜態(tài)網(wǎng)站網(wǎng)站營銷、App設(shè)計(jì)企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)