1. 程式人生 > >MySql FIND_IN_SET, SUBSTRING_INDEX, REGEXP正則匹配等字串函式應用

MySql FIND_IN_SET, SUBSTRING_INDEX, REGEXP正則匹配等字串函式應用

Mysql資料庫提供了比較豐富的字串函式,如上文“談談Mysql 字串連線 CONCAT CONCAT_WS GROUP_CONCAT區別及使用場景”中提到的字串連線函式,本文繼續講述Mysql剩餘常用字串函式的使用注意事項及使用場景。

1、字串長度

  • CHAR_LENGTH(str)、CHARACTER_LENGTH(str)
    Returns the length of the string str, measured in characters. A multibyte character counts as a single character. This means that for a string containing five 2-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.
    返回字元數
  • LENGTH(str)
    Returns the length of the string str, measured in bytes. A multibyte character counts as multiple bytes. This means that for a string containing five 2-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.
    返回位元組長度,不同編碼返回結果不同。
select CHAR_LENGTH('中歐d');	->3
select
LENGTH('中歐d'); ->utf-8, 7 select LENGTH('中歐d'); ->gbk, 5

2、字串查詢

  • INSTR(str,substr)
    Returns the position of the first occurrence of substring substr in string str. This is the same as the two-argument form of LOCATE(), except that the order of the arguments is reversed.
    找到返回>0的值(從1開始計數),否則返回0
    引數位置互換,等同LOCATE(substr,str)
  • LOCATE(substr,str),LOCATE(substr,str,pos)
    The first syntax returns the position of the first occurrence of substring substr in string str. The second syntax returns the position of the first occurrence of substring substr in string str, starting at position pos. Returns 0 if substr is not in str. Returns NULL if substr or str is NULL.
    找到返回>0(從1開始計數),否則返回0
    POSITION函式同LOCATE
SELECT INSTR('foobarbar', 'bar');		-> 4,第一次出現的位置
SELECT INSTR('xbar', 'foobar');		-> 0
SELECT LOCATE('bar', 'foobarbar');		-> 7
SELECT LOCATE('xbar', 'foobar');		-> 0
  • FIND_IN_SET(str,strlist)
    Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of
    N substrings. A string list is a string composed of substrings separated by , characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (,) character.
    查詢str是否在strlist以逗號連線的字串中,str不能包含逗號,沒找到返回0,找到返回>0值
SELECT FIND_IN_SET('b','a,b,c,d');		-> 2

使用場景:
在之前的博文:“談談企業資訊系統tag標籤資料庫設計及基於多選元件bootstrap-select的實現”中曾提到FIND_IN_SET函式使用,如經銷商渠道型別是tag標籤,有:傳統渠道、KA渠道、特通渠道等。查詢傳統渠道經銷商,用FIND_IN_SET函式如下:

select * from sale_customers where FIND_IN_SET('傳統渠道', tag_name) > 0
  • REGEXP:expr REGEXP pat, expr RLIKE pat
    字串正則匹配,匹配上返回1,否則返回0
mysql> SELECT 'Monty!' REGEXP '.*'; -> 1, 任意字元
mysql> SELECT 'new*\n*line' REGEXP 'new\\*.\\*line'; -> 1 \\*轉義
mysql> SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A'; -> 1 0
mysql> SELECT 'a' REGEXP '^[a-d]'; -> 1, a-d字元開頭

更多正則相關內容,請參考:Mysql參考手冊5.7 13.5.2 Regular Expressions
因為Mysql字串替換(replace)時不能使用正則,實際上正則使用率不是很高。

3、字串擷取

  • SUBSTRING(str,pos),SUBSTRING(strFROMpos),SUBSTRING(str,pos,len),
    SUBSTRING(str FROM pos FOR len)
    從pos位置開始擷取剩餘所有字串或擷取長度為len的字串。pos為負則從字串右邊開始計算位置,如-1則表示右邊第1個。
    SUBSTR() 同SUBSTRING
mysql> SELECT SUBSTRING('Quadratically',5); -> 'ratically',5個開始餘下所有字串
mysql> SELECT SUBSTRING('foobarbar' FROM 4); -> 'barbar',第4個開始餘下所有字串
mysql> SELECT SUBSTRING('Quadratically',5,6); -> 'ratica',第5個開始,擷取6個字元
mysql> SELECT SUBSTRING('Sakila', -3); -> 'ila',倒數第3個開始,擷取右側所有字元
mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2); -> 'ki',倒數第4個開始,擷取2個字元
  • 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.
    count > 0, 返回str按delim分割後左側第count開始左側字串
    count < 0, 返回str按delim分割後右側第count開始右側字串。
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2); -> 'www.mysql'
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2); -> 'mysql.com'

由於mysql沒有字串分割函式,SUBSTRING_INDEX是一個可行的替代函式,如擷取電話號碼、省市區等是一個不錯的選擇。