mysql的SUBSTRING_INDEX函數

可以用在例如 IP 位址 要取出最後主機位址時, 可以排序時使用.

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.
mysql> SELECT SUBSTRING_INDEX(‘www.mysql.com’, ‘.’, 2);
-> ‘www.mysql’
mysql> SELECT SUBSTRING_INDEX(‘www.mysql.com’, ‘.’, -2);
-> ‘mysql.com’

發表迴響