1. 程式人生 > >mysql 截取替換某個字符串

mysql 截取替換某個字符串

sel time ont ubd medium content strcmp tps expr

SELECT m.content,o.order_price,o.id,m.id
FROM scp_home_msg m 
INNER JOIN scp_order o ON m.link_id=o.id

技術分享圖片

把content 裏面的金額換成order_price

1、獲取金額

SELECT substring_index(content,實付¥,-1)  FROM  scp_home_msg WHERE id=1096;
-- 1000.00"}

2、獲取最後兩個字符

SELECT right(substring_index(content,實付¥,-1),2)  FROM  scp_home_msg WHERE
id=1096; -- "}

3、獲取完整金額

SELECT substring_index(substring_index(content,實付¥,-1),right(substring_index(content,實付¥,-1),2),1)  FROM  scp_home_msg WHERE id=1096;
-- 1000.00

4、得到結果

SELECT substring_index(substring_index(content,實付¥,-1),right(substring_index(content,實付¥,-1),2),1) jieGuo,
m.content content,o.order_price orderPrice,o.id orderId,m.id mId
FROM scp_home_msg m INNER JOIN scp_order o ON m.link_id=o.id;

技術分享圖片

5、獲取金額不一致的數據 進行比較

SELECT STRCMP(substring_index(substring_index(m.content,實付¥,-1),right(substring_index(m.content,實付¥,-1),2),1),o.order_price) jieGuo,
substring_index(substring_index(content,實付¥,-1),right(substring_index(content,
實付¥,-1),2),1) jj, o.order_price orderPrice,m.content content,o.id orderId,m.id mId FROM scp_home_msg m INNER JOIN scp_order o ON m.link_id=o.id;

技術分享圖片

6、得到結果不同數據

SELECT STRCMP(substring_index(substring_index(m.content,實付¥,-1),right(substring_index(m.content,實付¥,-1),2),1),o.order_price) jieGuo,
substring_index(substring_index(content,實付¥,-1),right(substring_index(content,實付¥,-1),2),1) jj,
o.order_price orderPrice,m.content content,o.id orderId,m.id mId
FROM scp_home_msg m 
INNER JOIN scp_order o ON m.link_id=o.id
WHERE STRCMP(substring_index(substring_index(m.content,實付¥,-1),right(substring_index(m.content,實付¥,-1),2),1),o.order_price) <> 0;

7、替換數據

SELECT m.content AS 替換前,REPLACE(content,substring_index(substring_index(content,實付¥,-1),right(substring_index(content,實付¥,-1),2),1),o.order_price) as 替換後,
o.order_price as 實付金額,substring_index(substring_index(content,實付¥,-1),right(substring_index(content,實付¥,-1),2),1) as 顯示金額,
o.id orderId,m.id mId
FROM scp_home_msg m 
INNER JOIN scp_order o ON m.link_id=o.id
WHERE substring_index(substring_index(m.content,實付¥,-1),right(substring_index(m.content,實付¥,-1),2),1) <> o.order_price;

總結:

1、replace(object, search,replace)
把object中出現search的全部替換為replace,select replace(‘www.163.com‘,‘w‘,‘Ww‘)--->WwW wWw.163.com
例:把表table中的name字段中的 aa替換為bb,update table set name=replace(name,‘aa‘,‘bb‘)

來自:http://www.jb51.net/article/25769.htm

2、mysql字符串截取 來自:https://www.cnblogs.com/shuaiandjun/p/7197450.html?utm_source=itdadao&utm_medium=referral

1left(str,index) 從左邊第index開始截取

2right(str,index)從右邊第index開始截取

3substring(str,index)當index>0從左邊開始截取直到結束  當index<0從右邊開始截取直到結束  當index=0返回空

4substring(str,index,len) 截取str,從index開始,截取len長度

5、substring_index(str,delim,count),str是要截取的字符串,delim是截取的字段 count是從哪裏開始截取(為0則是左邊第0個開始,1位左邊開始第一個選取左邊的,-1從右邊第一個開始選取右邊的

6、subdate(date,day)截取時間,時間減去後面的day

7、subtime(expr1,expr2)  時分秒expr1-expr2

mysql 截取替換某個字符串