Mysql函数:SUBSTRING_INDEX的使用

比如需要把IP提取前3段:SELECT SUBSTRING_INDEX('115.216.230.189', '.', 3);

SUBSTRING_INDEX:一个强大的截取函数,汇总一下就是:substring + indexOf/lastIndexOf 的组合,指定标识符往前或者往后截取 给个例子就懂了:

mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
        -> 'www.mysql'
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
        -> 'mysql.com'

查询Dash手册:
SUBSTRING_INDEX(str,delim,count)

Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

参考: Last index of a given substring in MySQL



blog comments powered by Disqus

Published

21 January 2017

Tags