1. 程式人生 > >MySQL學習之路6

MySQL學習之路6

MySQL函式

1.數學函式
1.1絕對值函式ABS(x),返回圓周率的函式PI(x)
例:
使用絕對值函式

mysql> SELECT ABS(-33),ABS(-3.3),ABS(2);
+----------+-----------+--------+
| ABS(-33) | ABS(-3.3) | ABS(2) |
+----------+-----------+--------+
|       33 |       3.3 |      2 |
+----------+-----------+--------+
1 row in set (0.11 sec)

使用圓周率函式

mysql> SELECT PI();
+----------+
| PI()     |
+----------+
| 3.141593 |
+----------+
1 row in set (0.10 sec)

1.2 平方根函式SQRT(x),求餘函式MOD(x)
例:
使用平方根函式:

mysql> SELECT SQRT(9),SQRT(40),SQRT(-49);
+---------+-------------------+-----------+
| SQRT(9) | SQRT(40)          | SQRT(-49) |
+---------+-------------------+-----------+
|       3 | 6.324555320336759 |      NULL |
+---------+-------------------+-----------+
1 row in set (0.10 sec)

發現負數沒有平方根。

使用求餘函式:

mysql> SELECT MOD(31,8),MOD(234,10),MOD(45.5,6);
+-----------+-------------+-------------+
| MOD(31,8) | MOD(234,10) | MOD(45.5,6) |
+-----------+-------------+-------------+
|         7 |           4 |         3.5 |
+-----------+-------------+-------------+
1 row in set (0.00 sec)

1.3 取整函式CEIL(x),CEILING(x),FLOOR(x)
CEIL(x),CEILING(x)返回不小於x的最小整數值,返回值轉化為一個BIGINT。
FLOOR(x)返回不大於x的最大整數值,返回值轉化為一個BEGINT。

1.4 獲取隨機數函式RAND(x)、RAND(x)
RAND(x)返回一個浮點值v,範圍在0~1之間。若 已指定一個整數引數x,則它被用作種子值,用來產生重複序列。
例:使用RAND()函式產生隨機數,輸入如下語句:

mysql> SELECT RAND(),RAND(),RAND();
+---------------------+--------------------+--------------------+
| RAND()              | RAND()             | RAND()             |
+---------------------+--------------------+--------------------+
| 0.18683199322487412 | 0.5247255373045109 | 0.0631317375815769 |
+---------------------+--------------------+--------------------+
1 row in set (0.55 sec)

用RAND(x)函式產生隨機數:

mysql> SELECT RAND(10),RAND(10),RAND(11);
+--------------------+--------------------+-------------------+
| RAND(10)           | RAND(10)           | RAND(11)          |
+--------------------+--------------------+-------------------+
| 0.6570515219653505 | 0.6570515219653505 | 0.907234631392392 |
+--------------------+---

1.5 四捨五入函式ROUND(x),ROUND(x,y)和TRUNCATE(x,y)
例:用ROUND(x)函式對運算元進行四捨五入操作,輸入如下語句:

mysql> SELECT ROUND(-1.14),ROUND(-1.67),ROUND(1.14),ROUND(1.66);
+--------------+--------------+-------------+-------------+
| ROUND(-1.14) | ROUND(-1.67) | ROUND(1.14) | ROUND(1.66) |
+--------------+--------------+-------------+-------------+
|           -1 |           -2 |           1 |           2 |
+--------------+--------------+-------------+-------------+
1 row in set (0.19 sec)

返回最接近x的整數。

例:使用ROUND(x,y)函式對運算元進行四捨五入操作:

mysql> SELECT ROUND(1.38,1),ROUND(1.38,0),ROUND(232.38,-1),ROUND(232.38,-2);
+---------------+---------------+------------------+------------------+
| ROUND(1.38,1) | ROUND(1.38,0) | ROUND(232.38,-1) | ROUND(232.38,-2) |
+---------------+---------------+------------------+------------------+
|           1.4 |             1 |              230 |              200 |
+---------------+---------------+------------------+------------------+
1 row in set (0.03 sec)
mysql> SELECT ROUND(236.38,-1);
+------------------+
| ROUND(236.38,-1) |
+------------------+
|              240 |
+------------------+
1 row in set (0.00 sec)

提示:y為負數時,保留的小數點左邊的相應位數直接儲存為0,不進行四捨五入。

TRUNCATE(x,y)返回被捨去小數點後y位的數字x;若y為0,不保留小數;若y為負數,則將左起第y位開始後面的所有數歸零。
TRUNCATE(x,y)直接擷取,不進行四捨五入。

mysql> SELECT TRUNCATE(1.31,1),TRUNCATE(1.99,1),TRUNCATE(1.99,-1),TRUNCATE(1.99,0);
+------------------+------------------+-------------------+------------------+
| TRUNCATE(1.31,1) | TRUNCATE(1.99,1) | TRUNCATE(1.99,-1) | TRUNCATE(1.99,0) |
+------------------+------------------+-------------------+------------------+
|              1.3 |              1.9 |                 0 |                1 |
+------------------+------------------+-------------------+------------------+
1 row in set (0.00 sec)

1.6 符號函式SIGN(x)
SIGN(x)返回引數的符號,x的值為負、零、或正時返回結果依次為-1,0、1。
例:使用SIGN函式返回引數的符號:

mysql> SELECT SIGN(-21),SIGN(0),SIGN(21);
+-----------+---------+----------+
| SIGN(-21) | SIGN(0) | SIGN(21) |
+-----------+---------+----------+
|        -1 |       0 |        1 |
+-----------+---------+----------+
1 row in set (0.02 sec)

1.7 冪運算函式POW(x,y)、POWER(x,y)和EXP(x)
POW(x,y)、POWER(x,y)函式返回x的y次乘方的結果值。
例:使用POW和POWER函式進行乘方運算:

mysql> SELECT POW(2,2),POWER(2,2),POW(2,-2),POWER(2,-2);
+----------+------------+-----------+-------------+
| POW(2,2) | POWER(2,2) | POW(2,-2) | POWER(2,-2) |
+----------+------------+-----------+-------------+
|        4 |          4 |      0.25 |        0.25 |
+----------+------------+-----------+-------------+
1 row in set (0.07 sec)

例:使用EXP函式計算e的乘方,輸入如下語句:

mysql> SELECT EXP(3),EXP(-3),EXP(0);_
+--------------------+----------------------+--------+
| EXP(3)             | EXP(-3)              | EXP(0) |
+--------------------+----------------------+--------+
| 20.085536923187668 | 0.049787068367863944 |      1 |
+--------------------+----------------------+--------+
1 row in set (0.05 sec)

1.8 對數運算函式LOG(x)和LOG10(x)
LOG(x)返回x的自然對數,LOG10(x)返回x的基數為10的對數。
例:使用LOG(x)函式計算自然對數:

mysql> SELECT LOG(3),LOG(-3);
+--------------------+---------+
| LOG(3)             | LOG(-3) |
+--------------------+---------+
| 1.0986122886681098 |    NULL |
+--------------------+---------+
1 row in set, 1 warning (0.32 sec)

例:使用LOG10(x)計算以10為基數的對數:

mysql> SELECT LOG10(2),LOG10(100),log10(-100);
+--------------------+------------+-------------+
| LOG10(2)           | LOG10(100) | log10(-100) |
+--------------------+------------+-------------+
| 0.3010299956639812 |          2 |        NULL |
+--------------------+------------+-------------+
1 row in set, 1 warning (0.02 sec)

1.9 角度與弧度相互轉換的函式RADIANS(x)和DEGREES(x)
RADIANS(x)將引數x由角度轉化為弧度。
例:使用RADIANS將角度轉換為弧度:

mysql> SELECT RADIANS(90),RADIANS(180);
+--------------------+-------------------+
| RADIANS(90)        | RADIANS(180)      |
+--------------------+-------------------+
| 1.5707963267948966 | 3.141592653589793 |
+--------------------+-------------------+
1 row in set (0.01 sec)

mysql> SELECT PI()/2;
+--------------+
| PI()/2       |
+--------------+
| 1.5707963268 |
+--------------+
1 row in set (0.14 sec)

DEGREES(x)將引數x轉換為角度。
例:使用DEGREES將弧度轉換為角度:

mysql> SELECT DEGREES(PI()),DEGREES(PI()/2);
+---------------+-----------------+
| DEGREES(PI()) | DEGREES(PI()/2) |
+---------------+-----------------+
|           180 |              90 |
+---------------+-----------------+
1 row in set (0.00 sec)

1.10 正弦函式SIN(x)和反正弦函式ASIN(x)
SIN(x)返回正弦,x為弧度值。
例:使用SIN函式計算正弦值:

mysql> SELECT SIN(1),ROUND(SIN(PI()));
+--------------------+------------------+
| SIN(1)             | ROUND(SIN(PI())) |
+--------------------+------------------+
| 0.8414709848078965 |                0 |
+--------------------+------------------+
1 row in set (0.14 sec)

round(x)返回接近x的整數。
ASIN(x)返回x的反正弦,即正弦為x的值。若x不在-1~1之間,返回NULL。
例:使用ASIN函式計算反正弦:

mysql> SELECT ASIN( 0.8414709848078965),asin(3);
+---------------------------+---------+
| ASIN( 0.8414709848078965) | asin(3) |
+---------------------------+---------+
|                         1 |    NULL |
+---------------------------+---------+
1 row in set (0.11 sec)

1.11 餘弦函式COS(x)和反餘弦函式ACOS(x)
COS(x)返回x的餘弦,x為弧度值。
例:使用COS函式計算餘弦值:

mysql> SELECT COS(0),COS(PI()),COS(1);
+--------+-----------+--------------------+
| COS(0) | COS(PI()) | COS(1)             |
+--------+-----------+--------------------+
|      1 |        -1 | 0.5403023058681398 |
+--------+-----------+--------------------+
1 row in set (0.02 sec)

ACOS(x)返回x的反餘弦。
例:使用ACOS(x)計算反餘弦值:

mysql> SELECT ACOS(1),ACOS(0),ROUND(ACOS(0.5403023058681398));
+---------+--------------------+---------------------------------+
| ACOS(1) | ACOS(0)            | ROUND(ACOS(0.5403023058681398)) |
+---------+--------------------+---------------------------------+
|       0 | 1.5707963267948966 |                               1 |
+---------+--------------------+---------------------------------+
1 row in set (0.11 sec)

1.12 正切函式、反正切函式和餘切函式
TAN(x)返回x的正切;
ATAN(x)返回x的反正切;
COT(x)返回x的餘切。
例:使用TAN(x)、ATAN(x)、COT(x)計算:

mysql> SELECT TAN(0.3),ROUND(TAN(PI()/4));
+---------------------+--------------------+
| TAN(0.3)            | ROUND(TAN(PI()/4)) |
+---------------------+--------------------+
| 0.30933624960962325 |                  1 |
+---------------------+--------------------+
1 row in set (0.00 sec)


mysql> SELECT ATAN( 0.30933624960962325),ATAN(1);
+----------------------------+--------------------+
| ATAN( 0.30933624960962325) | ATAN(1)            |
+----------------------------+--------------------+
|                        0.3 | 0.7853981633974483 |
+----------------------------+--------------------+
1 row in set (0.02 sec)


mysql> SELECT COT(0.3),1/TAN(0.3),COT(PI()/4);
+--------------------+--------------------+--------------------+
| COT(0.3)           | 1/TAN(0.3)         | COT(PI()/4)        |
+--------------------+--------------------+--------------------+
| 3.2327281437658275 | 3.2327281437658275 | 1.0000000000000002 |
+--------------------+--------------------+--------------------+
1 row in set (0.00 sec)

2.字串函式
2.1 計算字串字元數的函式和字串長度的函式
CHAR_LENGTH(str)返回字串str所包含的字元個數。一個多位元組字元算作一個單字元。
例:使用CHAR_LENGTH函式計算字串字元個數:

mysql> SELECT CHAR_LENGTH('DATE'),CHAR_LENGTH('egg');
+---------------------+--------------------+
| CHAR_LENGTH('DATE') | CHAR_LENGTH('egg') |
+---------------------+--------------------+
|                   4 |                  3 |
+---------------------+--------------------+
1 row in set (0.00 sec)

LENGTH(str)返回值字串的位元組長度。使用utf8(UNICODE的一種變長字元編碼,又稱萬國碼)編碼字符集時,一個漢字時3個位元組,一個數字或字母算一個位元組。
例:使用LENGTH函式計算字串長度:

mysql> SELECT LENGTH('DATE'),LENGTH('egg');
+----------------+---------------+
| LENGTH('DATE') | LENGTH('egg') |
+----------------+---------------+
|              4 |             3 |
+----------------+---------------+
1 row in set (0.00 sec)

2.2 合併字串函式CONTACT(s1,s2,…),CONTACT_WS(x,s1,s2,…)
CONTACT(s1,s2,…)返回結果為連線引數產生的字串,當有任意一個引數為NULL時,返回結果為NULL。
例:使用CONCAT函式連線字串:

mysql> SELECT CONCAT('My SQL','5.5'),CONCAT('My',NULL,'SQL');
+------------------------+-------------------------+
| CONCAT('My SQL','5.5') | CONCAT('My',NULL,'SQL') |
+------------------------+-------------------------+
| My SQL5.5              | NULL                    |
+------------------------+-------------------------+
1 row in set (0.12 sec)

CONTACT_WS(x,s1,s2,…),CONTACT_WS代表CONCAT With Separator,是CONCAT的特殊形式。第一個引數x是其他引數的分隔符,放在要連線的兩個字元之間。如果分隔符為NULL,則結果為NULL。函式會忽略任何分隔符引數後的NULL。
例:使用CONCAT_WS函式連線帶分隔符的字串:

mysql> SELECT CONCAT_WS('-','1st','2nd','3rd'),CONCAT_WS('*','1st',NULL,'3rd');
+----------------------------------+---------------------------------+
| CONCAT_WS('-','1st','2nd','3rd') | CONCAT_WS('*','1st',NULL,'3rd') |
+----------------------------------+---------------------------------+
| 1st-2nd-3rd                      | 1st*3rd                         |
+----------------------------------+---------------------------------+
1 row in set (0.10 sec)

2.3 替換字串的函式INSERT(s1,x,len,s2)
INSERT(s1,x,s2)返回字串s1,其子字串起始於x位置和被字串s2取代的字元len。如果x超過字串長度,返回原始字串,假如len的長度大於其他字串長度,則從位置x開始替換。若任何一個引數為NUL,則返回值為NULL。
例:使用INSERT函式進行字串替代操作:

mysql> SELECT INSERT('Quest',2,4,'What') AS col1,
    -> INSERT('Quest',-1,4,'What') AS col2,
    -> INSERT('Quest',3,100,'Wh')AS col3;
+-------+-------+------+
| col1  | col2  | col3 |
+-------+-------+------+
| QWhat | Quest | QuWh |
+-------+-------+------+
1 row in set (0.16 sec)

2.4 字母大小寫轉換函式
LOWER(str)或者LCASE(str)可以將字串str中的字母字元全部轉換成小寫字母。

例:使用LOWER(str)或者LCASE(str)函式:

mysql> SELECT LOWER('BEAUTIFUL'),LCASE('Well');
+--------------------+---------------+
| LOWER('BEAUTIFUL') | LCASE('Well') |
+--------------------+---------------+
| beautiful          | well          |
+--------------------+---------------+
1 row in set (0.12 sec)

UPPER(str)或者UCASE(str)將字母全部轉換為大寫字母。
例:使用字母轉大寫函式UPPER(str)或者UCASE(str):

mysql> SELECT UPPER('black'),UCASE('SFDdds');
+----------------+-----------------+
| UPPER('black') | UCASE('SFDdds') |
+----------------+-----------------+
| BLACK          | SFDDDS          |
+----------------+-----------------+
1 row in set (0.10 sec)

2.5 獲取指定長度的字串的函式LEFT(s,n)和RIGHT(s,n)
LEFT(s,n)返回字串s開始的最左邊n個字元;
RIGHT(s,n)返回字串s最右邊n個字元。
例:使用函式LEFT(s,n)和RIGHT(s,n):

mysql> SELECT LEFT('football',5),RIGHT('FOOTBALL',5);
+--------------------+---------------------+
| LEFT('football',5) | RIGHT('FOOTBALL',5) |
+--------------------+---------------------+
| footb              | TBALL               |
+--------------------+---------------------+
1 row in set (0.00 sec)

2.6 填充字串的函式LPAD(s1,len,s2)和RPAD(s1,len,s2)
LPAD(s1,len,s2)返回字串s1,其左邊由字串s2填補到len字元長度。若s1的長度大於len,則返回值被縮短至len字元。
例:使用LPAD函式對字串進行填充操作:

mysql> SELECT LPAD('HELLO',4,'??'),LPAD('HELLO',10,'??');
+----------------------+-----------------------+
| LPAD('HELLO',4,'??') | LPAD('HELLO',10,'??') |
+----------------------+-----------------------+
| HELL                 | ?????HELLO            |
+----------------------+-----------------------+
1 row in set (0.02 sec)

RPAD用法與LPAD相似,只是從右邊開始。
例:使用RPAD函式對字串進行填充:

mysql> SELECT RPAD('HELLO',4,'?'),RPAD('HELLO',10,'?');
+---------------------+----------------------+
| RPAD('HELLO',4,'?') | RPAD('HELLO',10,'?') |
+---------------------+----------------------+
| HELL                | HELLO?????           |
+---------------------+----------------------+
1 row in set (0.10 sec)

2.7 刪除空格的函式LTRIM(s)、RTRIM(s)、TRIM(s)
LTRIM(s):返回字串s,字串左側空格字元被刪除。
RTRIM(s):返回字串s,字串右側空格字元被刪除。
TRIM(S):刪除字串s兩側的空格。
例:使用刪除空格的函式LTRIM(s)、RTRIM(s)、TRIM(s):

LTRIM:

mysql> SELECT LTRIM('    BOOK    '),CONCAT('(',LTRIM('  BOOK   '),')');
+-----------------------+------------------------------------+
| LTRIM('    BOOK    ') | CONCAT('(',LTRIM('  BOOK   '),')') |
+-----------------------+------------------------------------+
| BOOK                  | (BOOK   )                          |
+-----------------------+------------------------------------+
1 row in set (0.13 sec)

RTRIM:

mysql> SELECT RTRIM('    BOOK    '),CONCAT('(',RTRIM('  BOOK   '),')');
+-----------------------+------------------------------------+
| RTRIM('    BOOK    ') | CONCAT('(',RTRIM('  BOOK   '),')') |
+-----------------------+------------------------------------+
|     BOOK              | (  BOOK)                           |
+-----------------------+------------------------------------+
1 row in set (0.00 sec)

TRIM:

mysql> SELECT TRIM('    BOOK    '),CONCAT('(',TRIM('  BOOK   '),')');
+----------------------+-----------------------------------+
| TRIM('    BOOK    ') | CONCAT('(',TRIM('  BOOK   '),')') |
+----------------------+-----------------------------------+
| BOOK                 | (BOOK)                            |
+----------------------+-----------------------------------+
1 row in set (0.00 sec)

2.9 重複生成字串的函式REPEAT(s,n)
REPEAT(s,n)返回一個由重複的字串s組成的字串,重複n。若n<=0,則返回一個空字串;若s或n為NULL,則返回NULL。
例:使用REPEAT函式重複生成相同的字串:

mysql> SELECT REPEAT('ASDSD',6),REPEAT('SAFFG',0),REPEAT('DAG',NULL),REPEAT(NULL,3);
+--------------------------------+-------------------+--------------------+----------------+
| REPEAT('ASDSD',6)              | REPEAT('SAFFG',0) | REPEAT('DAG',NULL) | REPEAT(NULL,3) |
+--------------------------------+-------------------+--------------------+----------------+
| ASDSDASDSDASDSDASDSDASDSDASDSD |                   | NULL               | NULL           |
+--------------------------------+-------------------+--------------------+----------------+
1 row in set (0.11 sec)

2.10 空格函式SPACE(n)和替換函式REPLACE(s,s1,s2)
SPACE(n)返回一個由n個空格組成的字串;
REPLACE(s,s1,s2)使用s2代替s中的所有s1。
例:使用空格函式SPACE(n)和替換函式REPLACE(s,s1,s2)

mysql> SELECT CONCAT('(',SPACE(4),')'),REPLACE('XXX.MYSQL.COM','X','W');
+--------------------------+----------------------------------+
| CONCAT('(',SPACE(4),')') | REPLACE('XXX.MYSQL.COM','X','W') |
+--------------------------+----------------------------------+
| (    )                   | WWW.MYSQL.COM                    |
+--------------------------+----------------------------------+
1 row in set (0.06 sec)

mysql> SELECT CONCAT('(',SPACE(4),')'),REPLACE('XXX.MYSQL.COM','XX','WW');
+--------------------------+------------------------------------+
| CONCAT('(',SPACE(4),')') | REPLACE('XXX.MYSQL.COM','XX','WW') |
+--------------------------+------------------------------------+
| (    )                   | WWX.MYSQL.COM                      |
+--------------------------+------------------------------------+
1 row in set (0.00 sec)

2.11 比較字串大小的函式STRCMP(s1,s2)
例:

mysql> SELECT STRCMP('TXT','TXT2'),STRCMP('TXT2','TXT'),STRCMP('TXT','TXT');
+----------------------+----------------------+---------------------+
| STRCMP('TXT','TXT2') | STRCMP('TXT2','TXT') | STRCMP('TXT','TXT') |
+----------------------+----------------------+---------------------+
|                   -1 |                    1 |                   0 |
+----------------------+----------------------+---------------------+
1 row in set (0.14 sec)

2.12 獲取子串的函式SUBSTRING(s,n,len)和MID(s,n,len)
例:使用SUBSTRING函式獲取指定位置處的字串:

mysql> SELECT SUBSTRING('BREAKFAST',5)  AS COL1,
    -> SUBSTRING('BREAKFAST',5,3) AS COL2,
    -> SUBSTRING('LUNCH',-3)  AS COL3;
+-------+------+------+
| COL1  | COL2 | COL3 |
+-------+------+------+
| KFAST | KFA  | NCH  |
+-------+------+------+
1 row in set (0.00 sec)

例:使用MID函式獲取指定位置處的子字串:

mysql> SELECT MID('BREAKFAST',5)  AS COL1,
    -> MID('BREAKFAST',5,3)  AS COL2,
    -> MID('BREAKFAST',-5)  AS COL3,
    -> MID('BREAKFAST',-5,3)  AS COL4;
+-------+------+-------+------+
| COL1  | COL2 | COL3  | COL4 |
+-------+------+-------+------+
| KFAST | KFA  | KFAST | KFA  |
+-------+------+-------+------+
1 row in set (0.00 sec)

2.13 匹配子串開始位置的函式
LOCATE(str1,str)、POSITION(str1 IN str)和INSTR(str,str1)三個函式的作用是相同的,返回子字串str1在str中的開始位置。
例:使用LOCATE(str1,str)、POSITION(str1 IN str)和INSTR(str,str1)三個函式:

mysql> SELECT LOCATE('BALL','FOOTBALL'),POSITION('BALL' IN 'FOOTBALL'),INSTR('FOOTBALL','BALL');
+---------------------------+--------------------------------+--------------------------+
| LOCATE('BALL','FOOTBALL') | POSITION('BALL' IN 'FOOTBALL') | INSTR('FOOTBALL','BALL') |
+---------------------------+--------------------------------+--------------------------+
|                         5 |                              5 |                        5 |
+---------------------------+--------------------------------+--------------------------+
1 row in set (0.11 sec)

2.14 字串逆序的函式REVERSE(s)
函式REVERSE(s)將字串s反轉。
例:使用函式REVERSE(s)反轉字串:

mysql> select reverse('abc');
+----------------+
| reverse('abc') |
+----------------+
| cba            |
+----------------+
1 row in set (0.00 sec)

2.16 返回指定字串位置的函式 FIELD(s,s1,s2,…)
FIELD(s,s1,s2,…)返回字串s在列表s1,s2,…中第一次出現的位置。
例:使用FIELD函式返回指定字串第一次出現的位置:

mysql> SELECT FIELD('Hi','hihi','Hi','Hey','bas') AS col1,
    -> FIELD('Hi','Hey','Lo','fdjkhg') as col2;
+------+------+
| col1 | col2 |
+------+------+
|    2 |    0 |
+------+------+
1 row in set (0.00 sec)

2.17 返回子串位置的函式FIND_IN_SET(s1,s2)
FIND_IN_SET(s1,s2)返回s1在字串列表s2中出現的位置。
例:使用FIND_IN_SET( )函式返回子字串在字串列表中的位置:

mysql> SELECT FIND_IN_SET('HI','HI,DF,DOF');
+-------------------------------+
| FIND_IN_SET('HI','HI,DF,DOF') |
+-------------------------------+
|                             1 |
+-------------------------------+
1 row in set (0.00 sec)

2.18 選取字串的函式MAKE_SET(x,s1,s2,…)
MAKE_SET(x,s1,s2,…)返回由x的二進位制數指定的相應位的字串組成的字串,s1對應位元1,s2對應位元01以此類推。s1,s2,…中的NULL值不會被新增到結果中。
例:只用MAKE_SET(x,s1,s2,…)選取指定字串:

mysql> SELECT MAKE_SET(1,'A','B','C') AS COL1,
    ->  MAKE_SET(1,'A','B','C') AS COL1,
    -> MAKE_SET(1|4,'HELLO','KJJ',NULL,'WORD') AS COL3,
    ->  MAKE_SET(0,'HELLO','WORD') AS COL4;
+------+------+-------+------+
| COL1 | COL1 | COL3  | COL4 |
+------+------+-------+------+
| A    | A    | HELLO |      |
+------+------+-------+------+
1 row in set (0.12 sec)

 1的二進位制位為0001,4的二進位制位為0100,1與4操作後得0101,從右到左第1位和第3位為1。

3. 日期和時間函式
3.1 獲取當前日期的函式和獲取當前時間的函式
CURDATE()和CURRENT_DATE()返回當前日期。
CURTIME()和CURRENT_TIME()返回當前時間。

mysql> SELECT CURDATE(),CURRENT_DATE(),CURDATE()+0;
+------------+----------------+-------------+
| CURDATE()  | CURRENT_DATE() | CURDATE()+0 |
+------------+----------------+-------------+
| 2018-12-14 | 2018-12-14     |    20181214 |
+------------+----------------+-------------+
1 row in set (0.00 sec)
mysql> SELECT CURTIME(),CURRENT_TIME(),CURTIME()+0;
+-----------+----------------+-------------+
| CURTIME() | CURRENT_TIME() | CURTIME()+0 |
+-----------+----------------+-------------+
| 13:28:46  | 13:28:46       |      132846 |
+-----------+----------------+-------------+
1 row in set (0.10 sec)

3.2 獲取當前日期和時間函式
獲取當前日期和時間的函式有:CURRENT_TIMESTAMP()、LOCALTIME()、NOW()和SYSDATE()。

mysql> SELECT CURRENT_TIMESTAMP(),LOCALTIME(),NOW(),SYSDATE();
+---------------------+---------------------+---------------------+---------------------+
| CURRENT_TIMESTAMP() | LOCALTIME()         | NOW()               | SYSDATE()           |
+---------------------+---------------------+---------------------+---------------------+
| 2018-12-14 13:31:12 | 2018-12-14 13:31:12 | 2018-12-14 13:31:12 | 2018-12-14 13:31:12 |
+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.11 sec)

3.3 UNIX時間戳函式
UNIX_TIMESTAMP(date),若無引數呼叫,返回一個Unix時間戳(‘1970-01-01 00:0000’ GMT之後的秒數)作為無符號整數。GMT為格林尼治標準時間。若用date來呼叫UNIX_TIMESTAMP(),它會將引數值以GMT後的秒數的形式返回。
使用FROM_UNIXTIME(date)函式可以把Unix時間戳返回普通格式。

mysql> SELECT UNIX_TIMESTAMP(),UNIX_TIMESTAMP(NOW()),NOW();
+------------------+-----------------------+---------------------+
| UNIX_TIMESTAMP() | UNIX_TIMESTAMP(NOW()) | NOW()               |
+------------------+-----------------------+---------------------+
|       1544765873 |            1544765873 | 2018-12-14 13:37:53 |
+------------------+-----------------------+---------------------+
1 row in set (0.03 sec)

mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()));
+--------------------------------------+
| FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())) |
+--------------------------------------+
| 2018-12-14 13:38:36                  |
+--------------------------------------+
1 row in set (0.12 sec)

3.4 返回UTC日期和返回UTC時間函式
UTC_DATE()返回當前時區日期值。
UTC_TIME()返回當前時區時間值。

mysql> SELECT UTC_DATE(),UTC_TIME();
+------------+------------+
| UTC_DATE() | UTC_TIME() |
+------------+------------+
| 2018-12-14 | 05:41:52   |
+------------+------------+
1 row in set (0.00 sec)

3.5 獲取月份的函式MONTH(date)和MONTHNAME(date)
MONTH(date)返回date對應的月份。
MONTHNAME(date)返回日期date對應月份的英文全名。

mysql> SELECT MONTH('2011-02-13'),MONTHNAME('2011-02-13');
+---------------------+-------------------------+
| MONTH('2011-02-13') | MONTHNAME('2011-02-13') |
+---------------------+-------------------------+
|                   2 | February                |
+---------------------+-------------------------+
1 row in set (0.07 sec)

3.6 獲取星期的函式DAYNAME(date)、DAYOFWEEK(d)和WEEKDAY(d)
DAYOFWEEK(d):1

mysql> SELECT DAYNAME('2011-02-13'),DAYOFWEEK('2011-02-13'),WEEKDAY('2011-02-13');
+-----------------------+-------------------------+-----------------------+
| DAYNAME('2011-02-13') | DAYOFWEEK('2011-02-13') | WEEKDAY('2011-02-13') |
+-----------------------+-------------------------+-----------------------+
| Sunday                |                       1 |                     6 |
+-----------------------+-------------------------+-----------------------+
1 row in set (0.11 sec)

3.7 獲取星期數的函式
WEEK(d)計算日期d是一年中的第幾周,WEEK()預設引數為0,代表一週中的第一天為週日,若1代表第一天為週一。
檢視default_week_format系統自變數的值:

mysql> SELECT @@default_week_format;
+-----------------------+
| @@default_week_format |
+-----------------------+
|                     0 |
+-----------------------+
1 row in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'default_week_format';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| default_week_format | 0     |
+---------------------+-------+
1 row in set, 6 warnings (0.00 sec)
自己設定值:
mysql> SET GLOBAL default_week_format=1;
Query OK, 0 rows affected (0.00 sec)

WEEKOFYEAR(d)計算某天位於一年中的第幾周。相當於WEEK(d,3)

3.8 獲取天數的函式DAYOFYEAR(d)和DAYOFMONTH(d)
DAYOFYEAR(d)返回d是一年中的第幾天。
DAYOFMONTH(d)返回d是一個月中的第幾天。

3.9 獲取年份、季度、小時、分鐘和秒鐘的函式
YEAR(date)返回date對應的年份。
QUARTER(date)返回date對應一年中的季度值。
MINUTE(time)返回time對應的分鐘數。
SECOND(time)返回time對應的秒數。

4. 條件判斷函式
4.1 IF(expr,v1,v2)函式

mysql> SELECT IF(1>2,2,3),
    -> IF(1<2,'yes','no'),
    -> IF(STRCMP('test','test1'),'no','yes');
+-------------+--------------------+---------------------------------------+
| IF(1>2,2,3) | IF(1<2,'yes','no') | IF(STRCMP('test','test1'),'no','yes') |
+-------------+--------------------+---------------------------------------+
|           3 | yes                | no                                    |
+-------------+--------------------+---------------------------------------+
1 row in set (0.07 sec)

4.2 IFNULL(v1,v2)函式
如果v1不為NULL,則IFNULL返回v1;否則返回v2。

4.3 CASE函式

mysql> SELECT CASE 2 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'more' END;
+------------------------------------------------------------+
| CASE 2 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'more' END |
+------------------------------------------------------------+
| two                                                        |
+------------------------------------------------------------+
1 row in set (0.00 sec)

5. 系統資訊函式
5.1 獲取MySQL版本號、連線數和資料庫名的函式

VERSION()返回指示伺服器版本的字串。(utf8字符集)

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.14    |
+-----------+
1 row in set (0.00 sec)

CONNECTION_ID()返回MySQL伺服器當前連線的次數,每個連線都有各自唯一的ID。

mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
|               6 |
+-----------------+
1 row in set (0.12 sec)

SHOW PROCESSLIST;SHOW FULL PROCESSLIST;
processlist命令的輸出結果顯示有哪些執行緒正在執行,可以檢視當前所有的連線數和連線狀態,幫助識別出有問題的查詢語句等。


mysql> SHOW PROCESSLIST;
+----+------+-----------------+---------+---------+------+----------+------------------+
| Id | User | Host            | db      | Command | Time | State    | Info             |
+----+------+-----------------+---------+---------+------+----------+------------------+
|  6 | root | localhost:52733 | test_db | Query   |    0 | starting | SHOW PROCESSLIST |
+----+------+-----------------+---------+---------+------+----------+------------------+
1 row in set (0.11 sec)

DATABASE(),SCHEMA()函式返回使用utf8字符集的預設(當前)資料庫名。

mysql> SELECT DATABASE(),SCHEMA();
+------------+----------+
| DATABASE() | SCHEMA() |
+------------+----------+
| test_db    | test_db  |
+------------+----------+
1 row in set (0.00 sec)

5.2 獲取使用者名稱的函式
USER()、CURRENT_USER、CURRENT_USER()、SYSTEM_USER()和SESSION_USER()。返回MySQL伺服器驗證的使用者名稱和主機名組合。

mysql> SELECT USER(),CURRENT_USER(),SYSTEM_USER();
+----------------+----------------+----------------+
| USER()         | CURRENT_USER() | SYSTEM_USER()  |
+----------------+----------------+----------------+
| [email protected] | [email protected] | [email protected]host |
+----------------+----------------+----------------+
1 row in set (0.10 sec)

5.3 獲取最後一個自動生成的ID值的函式
LAST_INSERT_ID()自動返回最後一個INSERT或UPDATE為AUTO_INCREMENT列設定的第一個發生的值。
一次插入一條記錄,返回值為最後一條插入記錄的id:

mysql> CREATE TABLE worker (Id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
    -> name VARCHAR(30));
Query OK, 0 rows affected (1.21 sec)

mysql> INSERT INTO worker VALUES(NULL,'jimy');
Query OK, 1 row affected (0.29 sec)

mysql> INSERT INTO worker VALUES(NULL,'Tom');
Query OK, 1 row affected (0.16 sec)

mysql> SELECT * FROM worker;
+----+------+
| Id | name |
+----+------+
|  1 | jimy |
|  2 | Tom  |
+----+------+
2 rows in set (0.00 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                2 |
+------------------+
1 row in set (0.10 sec)

同時插入多條記錄時,返回插入的第一行資料時產生的值:

mysql> INSERT INTO worker VALUES(NULL,'Keiven'),(NULL,'Michal'),(NULL,'Nick');
Query OK, 3 rows affected (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM worker;
+----+--------+
| Id | name   |
+----+--------+
|  1 | jimy   |
|  2 | Tom    |
|  3 | Keiven |
|  4 | Michal |
|  5 | Nick   |
+----+--------+
5 rows in set (0.00 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                3 |
+------------------+
1 row in set (0.00 sec)

6.其他函式
6.1 IP地址與數字相互轉換的函式

INET_ATON(expr)給出一個作為字串的網路地址的點地址表示,返回一個代表該地址數值的整數。地址可以是4或8位元地址。

mysql> SELECT INET_ATON('209.207.224.40');
+-----------------------------+
| INET_ATON('209.207.224.40') |
+-----------------------------+
|                  3520061480 |
+-----------------------------+
1 row in set (0.11 sec)

INET_NTOA(expr)返回地址:

mysql> SELECT INET_NTOA('3520061480');
+-------------------------+
| INET_NTOA('3520061480') |
+-------------------------+
| 209.207.224.40          |
+-------------------------+
1 row in set (0.00 sec)