mysql查询时字符串截取问题

版权声明:转载请说明 https://blog.csdn.net/qq_37694767/article/details/79977475
1、从左开始截取字符串
left(str, length)
说明:left(被截取字符串,截取的长度)
例:select left(string,200) as new_string from cs_news
2、从右开始截取字符串
right(str, length)
说明:right(被截取字符串,截取的长度)
例:select right(string,200) as new_string from cs_news
3、截取字符串
substring(str, pos)
substring(str, pos, length)
说明:substring(被截取字段,从第几位开始截取)
substring(被截取字段,从第几位开始截取,截取长度)
例:select substring(string,200) as new_string from cs_news
select substring(string,5,10) as new_string from cs_news

(注:如果位数是负数 如-5 则是从后倒数位数,到字符串结束或截取的长度)

substring_index(str,delim,count)
说明:substring_index(被截取字段,关键字,关键字出现的次数)

例:SELECT substring_index(member_ids,",",4) FROM `xg_meeting`

截取的字符串为',135,134,133,143,’,ID数据组,结果:",135,134,133"

截取到当前字符串第四个“,”之前的字符

(注:如果关键字出现的次数是负数 如-4 则是从后倒数,到字符串结束)

例:SELECT substring_index(member_ids,",",-4) FROM `xg_meeting`

截取的字符串为',135,134,133,143,’,ID数据组,结果:"134,133,143,"




阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页