MySQL字符串函数substring:字符串截取MySQL 字符串截取函数:left(), right(), substring(), substring_index()还有 mid(), sub。MySQL截取和拆分字符串函数用法示例?更多详情请大家跟着小编一起来看看吧!

MySQL截取和拆分字符串函数用法示例

MySQL截取和拆分字符串函数用法示例(1)

MySQL字符串函数substring:字符串截取

MySQL 字符串截取函数:left(), right(), substring(), substring_index()。还有 mid(), substr()。其中,mid(), substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。

1. 字符串截取:left(str, length)

mysql> select left(\'example\', 3);

+-------------------------+

| left(\'example\', 3) |

+-------------------------+

| exa |

+-------------------------+

2. 字符串截取:right(str, length)

mysql> select right(\'example\', 3);

+--------------------------+

| right(\'example\', 3) |

+--------------------------+

| com |

+--------------------------+

实例:

#查询某个字段后两位字符

select right(last3, 2) as last2 from historydata limit 10;

#从应该字段取后两位字符更新到另外一个字段

update `historydata` set `last2`=right(last3, 2);