mysql字串操作相關函數用法總結

來源:互聯網
上載者:User

標籤:cal   .com   指定   數字   localhost   字元   string   local   運算   

功能用法簡單例子一覽表
函數 功能 用法 例子
left() 從字串左邊為邊界返回相應長度的子字串 left(str, length) mysql> select left(‘vssf‘,3);+----------------+| left(‘vssf‘,3) |+----------------+| vss            |+----------------+1 row in set (0.00 sec)
right() 從字串右邊為邊界返回相應長度的子字串 rigth(str,length) mysql> select right(‘vssf‘,1);+-----------------+| right(‘vssf‘,1) |+-----------------+| f               |+-----------------+1 row in set (0.00 sec)
substring() 根據起始位置和長度返回相應的子字串 substring(str,pos,length) mysql> select substring(‘hello‘,1,1);+------------------------+| substring(‘hello‘,1,1) |+------------------------+| h                      |+------------------------+1 row in set (0.00 sec) mysql> select substring(‘hello‘,-4,1);+-------------------------+| substring(‘hello‘,-4,1) |+-------------------------+| e                       |+-------------------------+1 row in set (0.00 sec)
substring_index() 以某個字元(delim)為選取點,截取到該字元的所有字元.count為正數時從左到改字元,為負數時從該字元到右。如果找不到delim,則返回整個字串 substring_index(str,delim,count) mysql> select substring_index(‘hello.com.index‘,‘.‘,1);+------------------------------------------+| substring_index(‘hello.com.index‘,‘.‘,1) |+------------------------------------------+| hello                                    |+------------------------------------------+1 row in set (0.00 sec)
ASCII() 返回字串str最左邊字元的ascii碼值,如果是Null 字元串,則返回0,如果是null,則返回null。輸入是整型,也當做字串處理 ascii(str) mysql> select ascii(‘2‘);select ascii(2);+------------+| ascii(‘2‘) |+------------+|         50 |+------------+1 row in set (0.00 sec) +----------+| ascii(2) |+----------+|       50 |+----------+1 row in set (0.00 sec)
length() 計算字串長度 length(str) mysql> select length(user());+----------------+| length(user()) |+----------------+|             14 |+----------------+1 row in set (0.00 sec)
locate() 返回子串substr在字串str第一個出現的位置,從位置pos開始。如果substr不是在str裡面,返回0 LOCATE(substr,str,pos) mysql> select locate(‘123‘,‘141234‘,2);+--------------------------+| locate(‘123‘,‘141234‘,2) |+--------------------------+|                        3 |+--------------------------+1 row in set (0.00 sec)
instr() 返回子串substr在字串str中的第一個出現的位置。這與有2個參數形式的LOCATE()相同,除了參數被顛倒 INSTR(str,substr) mysql> select instr(‘12345‘,‘123‘);+----------------------+| instr(‘12345‘,‘123‘) |+----------------------+|                    1 |+----------------------+1 row in set (0.00 sec)
lpad() 返回字串str,左面用字串padstr填補直到str是len個字元長 LPAD (str,len,padstr) mysql> select LPAD(‘123‘,10,‘456‘);+----------------------+| LPAD(‘123‘,10,‘456‘) |+----------------------+| 4564564123           |+----------------------+1 row in set (0.00 sec)
ltrim() 返回刪除了其前置空白字元的字串str LTRIM(str) mysql> select ltrim(‘   121‘);+-----------------+| ltrim(‘   121‘) |+-----------------+| 121             |+-----------------+1 row in set (0.00 sec)
rtrim() 返回刪除了其拖後空白字元的字串str RTRIM(str) mysql> select rtrim(‘121  ‘);+----------------+| rtrim(‘121  ‘) |+----------------+| 121            |+----------------+1 row in set (0.00 sec)
ucase() 返回字串str,根據當前字元集映射(預設是ISO-8859-1 Latin1)把所有的字元改變成大寫。該函數對多位元組是可靠的 UCASE(str) mysql> select UCASE(‘sae‘);+--------------+| UCASE(‘sae‘) |+--------------+| SAE          |+--------------+1 row in set (0.00 sec)
lcase() 返回字串str,根據當前字元集映射(預設是ISO-8859-1 Latin1)把所有的字元改變成小寫。該函數對多位元組是可靠的。 LCASE(str) mysql> select LCASE(‘sS‘);+-------------+| LCASE(‘sS‘) |+-------------+| ss          |+-------------+1 row in set (0.00 sec)
make_set() 返回一個集合 (包含由“,”字元分隔的子串組成的一個字串),由相應的位在bits集合中的的字串組成。str1對應於位0,str2對應位1,等等。在str1, str2, ...中的NULL串不添加到結果中。 MAKE_SET(bits,str1,str2,...) mysql> select MAKE_SET(1|2,user,pass) from ctf;+-------------------------+| MAKE_SET(1|2,user,pass) |+-------------------------+| 1admin,11               || 4admin,pass            || admin123,123            || admin,test             |+-------------------------+4 rows in set (0.00 sec)
reverse() 返回顛倒字元順序的字串str REVERSE(str) mysql> select REVERSE(‘123‘);+----------------+| REVERSE(‘123‘) |+----------------+| 321            |+----------------+1 row in set (0.00 sec)
space() 返回由N個空白字元組成的一個字串 SPACE(N) mysql> select concat(1,SPACE(2),3);+----------------------+| concat(1,SPACE(2),3) |+----------------------+| 1  3                 |+----------------------+1 row in set (0.00 sec)
soundex 返回str的一個同音字串。聽起來“大致相同”的2個字串應該有相同的同音字串。一個“標準”的同音字串長是4個字元,但是SOUNDEX()函數返回一個任意長的字串。你可以在結果上使用SUBSTRING()得到一個“標準”的 同音串。所有非數字字母字元在給定的字串中被忽略。所有在A-Z之外的字元國際字母被當作母音。 SOUNDEX(str) mysql> select soundex(‘hello‘);+------------------+| soundex(‘hello‘) |+------------------+| H400             |+------------------+1 row in set (0.00 sec)
trim() 返回字串str,其所有remstr首碼或尾碼被刪除了。如果沒有修飾符BOTH、LEADING或TRAILING給出,BOTH被假定。如果remstr沒被指定,空格被刪除。 TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)  mysql> select trim(‘ dsf ‘);+---------------+| trim(‘ dsf ‘) |+---------------+| dsf           |+---------------+1 row in set (0.00 sec) mysql> select trim(leading ‘x‘ from ‘xxxbarxxx‘);+------------------------------------+| trim(leading ‘x‘ from ‘xxxbarxxx‘) |+------------------------------------+| barxxx                             |+------------------------------------+1 row in set (0.00 sec) mysql> select trim(trailing ‘x‘ from ‘xxxbarxxx‘);+-------------------------------------+| trim(trailing ‘x‘ from ‘xxxbarxxx‘) |+-------------------------------------+| xxxbar                              |+-------------------------------------+1 row in set (0.00 sec)
relace() 返回字串str,其字串from_str的所有出現由字串to_str代替 REPLACE(str,from_str,to_str) mysql> select replace(‘www.ok.com‘,‘w‘,‘q‘);+-------------------------------+| replace(‘www.ok.com‘,‘w‘,‘q‘) |+-------------------------------+| qqq.ok.com                    |+-------------------------------+1 row in set (0.00 sec)
repeat() 返回由重複countTimes次的字串str組成的一個字串。如果count <= 0,返回一個Null 字元串。如果str或count是NULL,返回NULL。 REPEAT(str,count) mysql> select repeat(‘223‘,2);+-----------------+| repeat(‘223‘,2) |+-----------------+| 223223          |+-----------------+1 row in set (0.00 sec)
insert() 返回字串str,在位置pos起始的子串且len個字元長得子串由字串newstr代替 INSERT(str,pos,len,newstr) mysql> select insert(‘dsfhdsa‘,3,4,‘ww‘);+----------------------------+| insert(‘dsfhdsa‘,3,4,‘ww‘) |+----------------------------+| dswwa                      |+----------------------------+1 row in set (0.00 sec)
elt() 如果N= 1,返回str1,如果N= 2,返回str2,等等。如果N小於1或大於參數個數,返回NULL。ELT()是FIELD()反運算 ELT(N,str1,str2,str3,...) mysql> select elt(1,user(),version());+-------------------------+| elt(1,user(),version()) |+-------------------------+| [email protected]          |+-------------------------+1 row in set (0.00 sec) mysql> select elt(2,user(),version());+-------------------------+| elt(2,user(),version()) |+-------------------------+| 5.5.47                  |+-------------------------+1 row in set (0.00 sec)
field() 返回str在str1, str2, str3, ...清單的索引。如果str沒找到,返回0。FIELD()是ELT()反運算 FIELD(str,str1,str2,str3,...) mysql> select field(‘v‘,‘he‘,‘hekl‘);+------------------------+| field(‘v‘,‘he‘,‘hekl‘) |+------------------------+|                      0 |+------------------------+1 row in set (0.00 sec)
mid() 根據起始位置和長度返回相應的子字串,與substring一樣的用法 MID(ColumnName, Start [, Length]) mysql> select mid(‘sfsdf‘,2,1);+------------------+| mid(‘sfsdf‘,2,1) |+------------------+| f                |+------------------+1 row in set (0.00 sec) mysql> select mid(‘sfsdf‘,2);+----------------+| mid(‘sfsdf‘,2) |+----------------+| fsdf           |+----------------+1 row in set (0.00 sec)
concat() 串連參數str1,str2等產生字串,若參數中包含有NULL,則直接返回null CONCAT(str1,str2,…) mysql> select concat(‘1‘,‘2‘);+-----------------+| concat(‘1‘,‘2‘) |+-----------------+| 12              |+-----------------+1 row in set (0.00 sec) mysql> select concat(‘1‘,‘2‘,null);+----------------------+| concat(‘1‘,‘2‘,null) |+----------------------+| NULL                 |+----------------------+1 row in set (0.00 sec)
concat_wd() 串連參數產生字串,可自訂分隔字元,參數中有null,也不會返回null,正常返回其他字串串連的結果 CONCAT_WS(separator,str1,str2,...) mysql> select concat_ws(‘,‘,‘2‘,‘3‘,null);+-----------------------------+| concat_ws(‘,‘,‘2‘,‘3‘,null) |+-----------------------------+| 2,3                         |+-----------------------------+1 row in set (0.00 sec)
group_concat() 串連參數產生字串,可定義分隔字元,可去重,可排序 group_concat([DISTINCT] 要串連的欄位 [Order BY ASC/DESC 排序欄位] [Separator ‘分隔字元‘]) mysql> select group_concat(user,pass) from ctf;+---------------------------------------------+| group_concat(user,pass)                     |+---------------------------------------------+| 1admin11,4adminpass,admin123123,admintest |+---------------------------------------------+1 row in set (0.00 sec)

mysql字串操作相關函數用法總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.