悠然哈哈哈

导航

mysql 截取替换某个字符串

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

 

posted on 2018-05-19 11:45  悠然886  阅读(1389)  评论(0编辑  收藏  举报