MySQL 內建函式

klvchen發表於2018-12-18

CHAR_LENGTH(str)

返回值為字串str 的長度,長度的單位為字元。一個多位元組字元算作一個單字元。

+------------------------+
| CHAR_LENGTH(`klvchen`) |
+------------------------+
|                      7 |
+------------------------+
1 row in set (0.00 sec)

mysql> SELECT CHAR_LENGTH(`你`);
+--------------------+
| CHAR_LENGTH(`你`)  |
+--------------------+
|                  1 |
+--------------------+
1 row in set (0.00 sec)

CONCAT(str1,str2,…)

字串拼接, 如有任何一個引數為NULL ,則返回值為 NULL。

mysql> SELECT CONCAT(`kl`,`v`,`chen`);
+-------------------------+
| CONCAT(`kl`,`v`,`chen`) |
+-------------------------+
| klvchen                 |
+-------------------------+
1 row in set (0.00 sec)

CONCAT_WS(separator,str1,str2,…)

字串拼接(自定義連線符)CONCAT_WS()不會忽略任何空字串。 (然而會忽略所有的 NULL)

mysql> SELECT CONCAT_WS(`_`,`chen`,`wj`);
+----------------------------+
| CONCAT_WS(`_`,`chen`,`wj`) |
+----------------------------+
| chen_wj                    |
+----------------------------+
1 row in set (0.01 sec)

mysql> SELECT CONCAT_WS(`_`,`chen`,` wj `, NULL, `hehe`);
+--------------------------------------------+
| CONCAT_WS(`_`,`chen`,` wj `, NULL, `hehe`) |
+--------------------------------------------+
| chen_ wj _hehe                             |
+--------------------------------------------+
1 row in set (0.00 sec)

CONV(N,from_base,to_base)

進位制轉換

mysql> SELECT CONV(`a`,16,2);
+----------------+
| CONV(`a`,16,2) |
+----------------+
| 1010           |
+----------------+
1 row in set (0.00 sec)

FORMAT(X,D)

將數字X 的格式寫為`#,###,###.##`,以四捨五入的方式保留小數點後 D 位, 並將結果以字串的形式返回。若 D 為 0, 則返回結果不帶有小數點,或不含小數部分。

mysql> SELECT FORMAT(1.23,4);
+----------------+
| FORMAT(1.23,4) |
+----------------+
| 1.2300         |
+----------------+
1 row in set (0.00 sec)

INSERT(str,pos,len,newstr)

str 原字串; pos 要替換位置其實位置(從1開始); len:替換的長度;newstr:需要替換的符串
簡潔概況為:先從 pos 處刪除 len 長度的字串,再由新的字串代替

mysql> SELECT INSERT(`klvchen`, 1, 1, `j`);
+------------------------------+
| INSERT(`klvchen`, 1, 1, `j`) |
+------------------------------+
| jlvchen                      |
+------------------------------+
1 row in set (0.00 sec)

mysql> SELECT INSERT(`klvchen`, 1, 1, `ja`);
+-------------------------------+
| INSERT(`klvchen`, 1, 1, `ja`) |
+-------------------------------+
| jalvchen                      |
+-------------------------------+
1 row in set (0.00 sec)

mysql> SELECT INSERT(`klvchen`, 1, 2, `a`);
+------------------------------+
| INSERT(`klvchen`, 1, 2, `a`) |
+------------------------------+
| avchen                       |
+------------------------------+
1 row in set (0.00 sec)

INSTR(str,substr)

返回字串 str 中子字串的第一個出現位置。

mysql> SELECT INSTR(`klvchen`,`l`);
+----------------------+
| INSTR(`klvchen`,`l`) |
+----------------------+
|                    2 |
+----------------------+
1 row in set (0.00 sec)

LOCATE(substr,str,pos)

獲取子序列索引位置

mysql> SELECT LOCATE(`l`,`klvchen`,1);
+-------------------------+
| LOCATE(`l`,`klvchen`,1) |
+-------------------------+
|                       2 |
+-------------------------+
1 row in set (0.01 sec)

LEFT(str,len)

返回字串 str 從開始的 len 位置的子序列字元。

mysql> SELECT LEFT(`klvchen`,2);
+-------------------+
| LEFT(`klvchen`,2) |
+-------------------+
| kl                |
+-------------------+
1 row in set (0.00 sec)

RIGHT(str,len)

從字串 str 開始,返回從後邊開始 len個字元組成的子序列

mysql> SELECT RIGHT(`klvchen`, 2);
+---------------------+
| RIGHT(`klvchen`, 2) |
+---------------------+
| en                  |
+---------------------+
1 row in set (0.00 sec)

LOWER(str)

字串轉化為小寫

mysql> SELECT LOWER(`KLVCHEN`);
+------------------+
| LOWER(`KLVCHEN`) |
+------------------+
| klvchen          |
+------------------+
1 row in set (0.00 sec)

UPPER(str)

字串轉化為大寫

mysql> SELECT UPPER(`klvchen`);
+------------------+
| UPPER(`klvchen`) |
+------------------+
| KLVCHEN          |
+------------------+
1 row in set (0.00 sec)

LTRIM(str)

返回字串 str ,左邊的空格字元被刪除。

mysql> SELECT LTRIM(`  klvchen`);
+--------------------+
| LTRIM(`  klvchen`) |
+--------------------+
| klvchen            |
+--------------------+
1 row in set (0.00 sec)

RTRIM(str)

返回字串 str ,結尾空格字元被被刪除。

mysql> SELECT RTRIM(`klvchen    `);
+----------------------+
| RTRIM(`klvchen    `) |
+----------------------+
| klvchen              |
+----------------------+
1 row in set (0.00 sec)

TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str) TRIM(remstr FROM] str)

返回字串 str , 其中所有remstr 字首和/或字尾都已被刪除。若分類符BOTH、LEADIN或TRAILING中沒有一個是給定的,則假設為BOTH 。 remstr 為可選項,在未指定情況下,可刪除空格。

mysql> SELECT TRIM(`   kl   `);
+------------------+
| TRIM(`   kl   `) |
+------------------+
| kl               |
+------------------+
1 row in set (0.00 sec)

mysql> SELECT TRIM(LEADING `x` FROM  `xxklxx`);
+----------------------------------+
| TRIM(LEADING `x` FROM  `xxklxx`) |
+----------------------------------+
| klxx                             |
+----------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TRIM(BOTH `x` FROM  `xxklxx`);
+-------------------------------+
| TRIM(BOTH `x` FROM  `xxklxx`) |
+-------------------------------+
| kl                            |
+-------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TRIM(trailing `x` FROM  `xxklxx`);
+-----------------------------------+
| TRIM(trailing `x` FROM  `xxklxx`) |
+-----------------------------------+
| xxkl                              |
+-----------------------------------+
1 row in set (0.00 sec)

REPEAT(str,count)

返回一個由重複的字串 str 組成的字串,字串str重複的次數等於count的值 。
若 count <= 0,則返回一個空字串;若str 或 count 為 NULL,則返回 NULL 。

mysql> SELECT REPEAT(`ha`,3);
+----------------+
| REPEAT(`ha`,3) |
+----------------+
| hahaha         |
+----------------+
1 row in set (0.00 sec)

REPLACE(str,from_str,to_str)

返回字串str 以及所有被字串to_str替代的字串from_str 。

mysql> SELECT REPLACE(`klvchel`, `l`, `i`);
+------------------------------+
| REPLACE(`klvchel`, `l`, `i`) |
+------------------------------+
| kivchei                      |
+------------------------------+
1 row in set (0.00 sec)

REVERSE(str)

返回字串 str ,順序和字元順序相反。

mysql> SELECT REVERSE(`klvchen`);
+--------------------+
| REVERSE(`klvchen`) |
+--------------------+
| nehcvlk            |
+--------------------+
1 row in set (0.00 sec)

SUBSTRING(str,pos,len)

獲取字串子序列

mysql> SELECT SUBSTRING(`klvchen`, 1, 3);
+----------------------------+
| SUBSTRING(`klvchen`, 1, 3) |
+----------------------------+
| klv                        |
+----------------------------+
1 row in set (0.00 sec)

mysql> SELECT SUBSTRING(`klvchen`, 3);
+-------------------------+
| SUBSTRING(`klvchen`, 3) |
+-------------------------+
| vchen                   |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT SUBSTRING(`klvchen` FROM 3);
+-----------------------------+
| SUBSTRING(`klvchen` FROM 3) |
+-----------------------------+
| vchen                       |
+-----------------------------+
1 row in set (0.00 sec)

mysql> SELECT SUBSTRING(`klvchen`, -3);
+--------------------------+
| SUBSTRING(`klvchen`, -3) |
+--------------------------+
| hen                      |
+--------------------------+
1 row in set (0.00 sec)

mysql> SELECT SUBSTRING(`klvchen`, -3, 2);
+-----------------------------+
| SUBSTRING(`klvchen`, -3, 2) |
+-----------------------------+
| he                          |
+-----------------------------+
1 row in set (0.00 sec)

mysql> SELECT SUBSTRING(`klvchen` FROM -3 FOR 2);
+------------------------------------+
| SUBSTRING(`klvchen` FROM -3 FOR 2) |
+------------------------------------+
| he                                 |
+------------------------------------+
1 row in set (0.01 sec)

相關文章