中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

五、MySQL函數

發布時間:2020-07-09 11:42:52 來源:網絡 閱讀:720 作者:少年不在了 欄目:MySQL數據庫

  函數表示對輸入參數值返回一個具有特定關系的值MySQL提供大量豐富的函數在進行數據庫管理以及數據的查詢和操作時將會經常用到各種函數。通過對數據的處理數據庫功能可以變得更加強大更加靈活地滿足不同需求。各類函數從功能主要分為以下幾類數學函數、字符串函數、日期和時間函數、條件判斷函數、系統信息函數和加密函數。

5.1、數學函數

絕對值函數ABS(x)

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

返回圓周率函數PI()

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

平方根函數SQRT(x)

mysql> SELECT SQRT(9),SQRT(30),SQRT(-30);
+---------+-------------------+-----------+
| SQRT(9) | SQRT(30)          | SQRT(-30) |
+---------+-------------------+-----------+
|       3 | 5.477225575051661 |      NULL |
+---------+-------------------+-----------+
1 row in set (0.00 sec)

求余函數MOD(x,y)

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

獲取整數函數CEIL(x)、CEILING(x)、FLOOR(x)

mysql> SELECT CEIL(-3.35),CEILING(3.35);   #返回不小于x的最小整數
+-------------+---------------+
| CEIL(-3.35) | CEILING(3.35) |
+-------------+---------------+
|          -3 |             4 |
+-------------+---------------+
1 row in set (0.00 sec)

mysql> SELECT FLOOR(-3.35),FLOOR(3.35);    #返回不大于x的最大整數
+--------------+-------------+
| FLOOR(-3.35) | FLOOR(3.35) |
+--------------+-------------+
|           -4 |           3 |
+--------------+-------------+
1 row in set (0.00 sec)

獲取隨機數函數RAND()、RAND(x)

mysql> SELECT RAND(),RAND(),RAND();        #返回0-1之間的隨機數
+--------------------+--------------------+--------------------+
| RAND()             | RAND()             | RAND()             |
+--------------------+--------------------+--------------------+
| 0.8727586752481373 | 0.6464434700519252 | 0.6139413552488585 |
+--------------------+--------------------+--------------------+
1 row in set (0.00 sec)

mysql> SELECT RAND(10),RAND(10),RAND(11);  #x用作種子值用來產生重復序列
+--------------------+--------------------+-------------------+
| RAND(10)           | RAND(10)           | RAND(11)          |
+--------------------+--------------------+-------------------+
| 0.6570515219653505 | 0.6570515219653505 | 0.907234631392392 |
+--------------------+--------------------+-------------------+
1 row in set (0.00 sec)

函數ROUND(x)、ROUND(x,y)和TRUNCATE(x,y)

mysql> SELECT ROUND(-1.14),ROUND(1.67),ROUND(1.15); #返回最接近x的整數對x值進行四舍五入
+--------------+-------------+-------------+
| ROUND(-1.14) | ROUND(1.67) | ROUND(1.15) |
+--------------+-------------+-------------+
|           -1 |           2 |           1 |
+--------------+-------------+-------------+
1 row in set (0.00 sec)

mysql> SELECT ROUND(-1.14,1),ROUND(1.67,0),ROUND(1.15,-1);
+----------------+---------------+----------------+
| ROUND(-1.14,1) | ROUND(1.67,0) | ROUND(1.15,-1) |
+----------------+---------------+----------------+
|           -1.1 |             2 |              0 |
+----------------+---------------+----------------+
1 row in set (0.00 sec)   #返回最接近x的整數值保留小數點后y位y為負數將保留x小數點左邊y位

mysql> SELECT TRUNCATE(1.31,1),TRUNCATE(1.99,1),TRUNCATE(1.99,0);
+------------------+------------------+------------------+
| TRUNCATE(1.31,1) | TRUNCATE(1.99,1) | TRUNCATE(1.99,0) |
+------------------+------------------+------------------+
|              1.3 |              1.9 |                1 |
+------------------+------------------+------------------+
1 row in set (0.00 sec)  #返回被舍去至小數點后y位的數字x

符號函數SIGN(x)

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

冪運算函數POW(x,y)、POWER(x,y)和EXP(x)

mysql> SELECT POW(2,2),POWER(2,2),POW(2,-2),POWER(2,-2);#返回x的y次方
+----------+------------+-----------+-------------+
| POW(2,2) | POWER(2,2) | POW(2,-2) | POWER(2,-2) |
+----------+------------+-----------+-------------+
|        4 |          4 |      0.25 |        0.25 |
+----------+------------+-----------+-------------+
1 row in set (0.04 sec)  

mysql> SELECT EXP(3),EXP(-3),EXP(0);   #返回以e為底的x次方
+--------------------+----------------------+--------+
| EXP(3)             | EXP(-3)              | EXP(0) |
+--------------------+----------------------+--------+
| 20.085536923187668 | 0.049787068367863944 |      1 |
+--------------------+----------------------+--------+
1 row in set (0.02 sec)

對數運算函數LOG(x)和LOG10(x)

mysql> SELECT LOG(3),LOG(-3);  #返回x的自然對數x相對于基數e的對數
+--------------------+---------+
| LOG(3)             | LOG(-3) |
+--------------------+---------+
| 1.0986122886681098 |    NULL |
+--------------------+---------+
1 row in set (0.00 sec)

mysql> SELECT LOG10(2),LOG10(100),LOG10(-100);  #返回x相對于基數10的對數
+--------------------+------------+-------------+
| LOG10(2)           | LOG10(100) | LOG10(-100) |
+--------------------+------------+-------------+
| 0.3010299956639812 |          2 |        NULL |
+--------------------+------------+-------------+
1 row in set (0.00 sec)

角度與弧度相互轉換的函數RADIANS(x)和DEGREES(x)

mysql> SELECT RADIANS(90),RADIANS(180);   #角度轉化為弧度
+--------------------+-------------------+
| RADIANS(90)        | RADIANS(180)      |
+--------------------+-------------------+
| 1.5707963267948966 | 3.141592653589793 |
+--------------------+-------------------+
1 row in set (0.00 sec)

mysql> SELECT DEGREES(PI()),DEGREES(PI()/2);#弧度轉化為角度
+---------------+-----------------+
| DEGREES(PI()) | DEGREES(PI()/2) |
+---------------+-----------------+
|           180 |              90 |
+---------------+-----------------+
1 row in set (0.00 sec)

正弦函數SIN(x)和反正弦函數ASIN(x)

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

mysql> SELECT ASIN(3),ASIN(0.84);
+---------+--------------------+
| ASIN(3) | ASIN(0.84)         |
+---------+--------------------+
|    NULL | 0.9972832223717998 |
+---------+--------------------+
1 row in set (0.00 sec)

余弦函數COS(x)和反余弦函數ACOS(x)

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

mysql> SELECT ACOS(1),ACOS(0.54),ACOS(0);
+---------+--------------------+--------------------+
| ACOS(1) | ACOS(0.54)         | ACOS(0)            |
+---------+--------------------+--------------------+
|       0 | 1.0003592173949747 | 1.5707963267948966 |
+---------+--------------------+--------------------+
1 row in set (0.00 sec)

正切函數TAN(X)、反正切函數ATAN(x)和余切函數COT(x)

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

mysql> SELECT ATAN(1),ATAN(0.393);   
+--------------------+---------------------+
| ATAN(1)            | ATAN(0.393)         |
+--------------------+---------------------+
| 0.7853981633974483 | 0.37445736689641174 |
+--------------------+---------------------+
1 row in set (0.00 sec)

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

5.2、字符串函數

計算字符串字符數CHAR_LENGTH(str)

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)

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

合并字符串函數CONCAT(s1,s2,...)、CONCAT_WS(x,s1,s2,)

mysql> SELECT CONCAT('My SQL','5.6'),CONCAT('My SQL','5.6',NULL);
+------------------------+-----------------------------+
| CONCAT('My SQL','5.6') | CONCAT('My SQL','5.6',NULL) |
+------------------------+-----------------------------+
| My SQL5.6              | NULL                        |
+------------------------+-----------------------------+
1 row in set (0.00 sec)  #連接多個字符串出現NULL時返回值為NULL

mysql> SELECT CONCAT_WS('-','My SQL','5.6'),CONCAT_WS('-','My SQL','5.6',NULL);
+-------------------------------+------------------------------------+
| CONCAT_WS('-','My SQL','5.6') | CONCAT_WS('-','My SQL','5.6',NULL) |
+-------------------------------+------------------------------------+
| My SQL-5.6                    | My SQL-5.6                         |
+-------------------------------+------------------------------------+
1 row in set (0.00 sec)  #以x為連接符連接多個字符串出現NULL時忽略NULL

替換字符串函數INSERT(s1,x,len,s2)

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

字母大小寫轉換函數LOWER(str)、UPPER(str)

mysql> SELECT LOWER('BUFF'),LCASE('Well');
+---------------+---------------+
| LOWER('BUFF') | LCASE('Well') |
+---------------+---------------+
| buff          | well          |
+---------------+---------------+
1 row in set (0.00 sec)

mysql> SELECT UPPER('buff'),UCASE('Well');
+---------------+---------------+
| UPPER('buff') | UCASE('Well') |
+---------------+---------------+
| BUFF          | WELL          |
+---------------+---------------+
1 row in set (0.00 sec)

獲取指定長度的字符串函數LEFT(s,n)和RIGHT(s,n)

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

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

填充字符串的函數LPAD(s1,len,s2)和RPAD(s1,len,s2)

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

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

刪除空格的函數LTRIM(s)、RTRIM(s)和TRIM(s)

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

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

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

刪除指定字符串的函數TRIM(s1 FROM s)

mysql> SELECT TRIM('XY' FROM 'ASHDLADHXYQWEPIXY');
+-------------------------------------+
| TRIM('XY' FROM 'ASHDLADHXYQWEPIXY') |
+-------------------------------------+
| ASHDLADHXYQWEPI                     |
+-------------------------------------+
1 row in set (0.00 sec)

重復生成字符串的函數REPEAT(s,n)

mysql> SELECT REPEAT('MYSQL',5);
+---------------------------+
| REPEAT('MYSQL',5)         |
+---------------------------+
| MYSQLMYSQLMYSQLMYSQLMYSQL |
+---------------------------+
1 row in set (0.00 sec)

空格函數SPACE(n)

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

替換函數REPLACE(s,s1,s2)

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

比較字符串大小的函數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.00 sec)

獲取字符串的函數SUBSTRING(s,n,len)和MID(s,n,len)

mysql> SELECT SUBSTRING('BREAKFAST',5) AS col1,  從第5個位置到結尾的字符串
    -> SUBSTRING('BREAKFAST',5,3) AS col2,       從第5個位置開始長度3的子字符串
    -> SUBSTRING('lunch',-3) AS col3,            從結尾開始第3個位置到字符串結尾
    -> SUBSTRING('lunch',-5,3) AS col4;          從結尾開始第5個位置長度為3的自字符串
+-------+------+------+------+
| col1  | col2 | col3 | col4 |
+-------+------+------+------+
| KFAST | KFA  | nch  | lun  |
+-------+------+------+------+
1 row in set (0.00 sec)

匹配子串開始位置的函數LOCATE(s1,s)、POSITION(s1 IN s)、INSTR(s,s1)

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

字符串逆序函數REVERSE(s)

mysql> SELECT REVERSE('ABCD');
+-----------------+
| REVERSE('ABCD') |
+-----------------+
| DCBA            |
+-----------------+
1 row in set (0.00 sec)

返回指定位置的字符串函數ELT(n,s1,s2,...)

mysql> SELECT ELT(3,'1TH','2TH','3TH'),ELT(3,'NET','OS');
+--------------------------+-------------------+
| ELT(3,'1TH','2TH','3TH') | ELT(3,'NET','OS') |
+--------------------------+-------------------+
| 3TH                      | NULL              |
+--------------------------+-------------------+
1 row in set (0.00 sec)

返回指定字符串位置的函數FIELD(s,s1,s2,...)

mysql> SELECT FIELD('HI','HIHI','HEY','HI','BAS') AS clo1,
    -> FIELD('HI','HEY','LO') AS clo2;
+------+------+
| clo1 | clo2 |
+------+------+
|    3 |    0 |
+------+------+
1 row in set (0.00 sec)

返回子串位置的函數FIND_IN_SET(s1,s2)

mysql> SELECT FIND_IN_SET('hi','hihi,hey,hi,bas');
+-------------------------------------+
| FIND_IN_SET('hi','hihi,hey,hi,bas') |
+-------------------------------------+
|                                   3 |
+-------------------------------------+
1 row in set (0.00 sec)

選取字符串的函數MAKE_SET(x,s1,s2,...)

mysql> SELECT MAKE_SET(1,'A','B','C') as col1, 
    -> MAKE_SET(1 | 4,'hello','nice','world') as col2, 
    -> MAKE_SET(1 | 4,'hello','nice',NULL,'world') as col3,
    -> MAKE_SET(0,'a','b','c') as col4;                        
+------+-------------+-------+------+
| col1 | col2        | col3  | col4 |
+------+-------------+-------+------+
| A    | hello,world | hello |      |
+------+-------------+-------+------+
1 row in set (0.00 sec)


5.3、日期和時間函數

獲取當前日期的函數CURDATE()、CURRENT_DATE()

mysql> SELECT CURDATE(),CURRENT_DATE(),CURDATE()+0;
+------------+----------------+-------------+
| CURDATE()  | CURRENT_DATE() | CURDATE()+0 |
+------------+----------------+-------------+
| 2017-08-01 | 2017-08-01     |    20170801 |
+------------+----------------+-------------+
1 row in set (0.00 sec)

獲取當前時間的函數CURTIME()、CURRENT_TIME()

mysql> SELECT CURTIME(),CURRENT_TIME(),CURTIME()+0;
+-----------+----------------+---------------+
| CURTIME() | CURRENT_TIME() | CURTIME()+0   |
+-----------+----------------+---------------+
| 18:54:38  | 18:54:38       | 185438.000000 |
+-----------+----------------+---------------+
1 row in set (0.00 sec)

獲取當前日期和時間的函數CURRENT_TIMESTAMP()、LOCALTIME()、NOW()、SYSDATE()

mysql> SELECT CURRENT_TIMESTAMP(),LOCALTIME(),NOW(),SYSDATE();
+---------------------+---------------------+---------------------+---------------------+
| CURRENT_TIMESTAMP() | LOCALTIME()         | NOW()               | SYSDATE()           |
+---------------------+---------------------+---------------------+---------------------+
| 2017-08-01 18:55:48 | 2017-08-01 18:55:48 | 2017-08-01 18:55:48 | 2017-08-01 18:55:48 |
+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)

UNIX時間戳函數

mysql> SELECT UNIX_TIMESTAMP(),UNIX_TIMESTAMP(NOW()),NOW();  
+------------------+-----------------------+---------------------+
| UNIX_TIMESTAMP() | UNIX_TIMESTAMP(NOW()) | NOW()               |
+------------------+-----------------------+---------------------+
|       1501585038 |            1501585038 | 2017-08-01 18:57:18 |
+------------------+-----------------------+---------------------+
1 row in set (0.00 sec)#返回UNIX格式時間即1970-01-01 00:00:00 之后的秒數

 
mysql> SELECT FROM_UNIXTIME('1466393937');  #將UNIX時間戳轉換為普通格式時間
+-----------------------------+
| FROM_UNIXTIME('1466393937') |
+-----------------------------+
| 2016-06-20 11:38:57         |
+-----------------------------+
1 row in set (0.00 sec)

返回UTC日期的函數UTC_DATE()

mysql> SELECT UTC_DATE(),UTC_DATE()+0;
+------------+--------------+
| UTC_DATE() | UTC_DATE()+0 |
+------------+--------------+
| 2017-08-01 |     20170801 |
+------------+--------------+
1 row in set (0.00 sec)

返回UTC時間的函數UTC_TIME()

mysql> SELECT UTC_TIME(),UTC_TIME()+0;
+------------+---------------+
| UTC_TIME() | UTC_TIME()+0  |
+------------+---------------+
| 11:01:31   | 110131.000000 |
+------------+---------------+
1 row in set (0.00 sec)

獲取月份的函數MONTH(date)、MONTHNAME(date)

mysql> SELECT MONTH('2017-07-13');
+---------------------+
| MONTH('2017-07-13') |
+---------------------+
|                   7 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT MONTHNAME('2017-07-13');
+-------------------------+
| MONTHNAME('2017-07-13') |
+-------------------------+
| July                    |
+-------------------------+
1 row in set (0.00 sec)

獲取星期的函數DAYNAME(d)、DAYOFWEEK(d)、WEEKDAY(d)

mysql> SELECT DAYNAME('2017-07-13');  #返回指定日期的星期
+-----------------------+
| DAYNAME('2017-07-13') |
+-----------------------+
| Thursday              |
+-----------------------+
1 row in set (0.00 sec)

mysql> SELECT DAYOFWEEK('2017-07-13');#返回d對應一周中的索引(1周日,2周一,...7周六)
+-------------------------+
| DAYOFWEEK('2017-07-13') |
+-------------------------+
|                       5 |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT WEEKDAY('2017-07-13');#返回d對應一工作日的索引(0周日,1周一,...6周六)
+-----------------------+
| WEEKDAY('2017-07-13') |
+-----------------------+
|                     3 |
+-----------------------+
1 row in set (0.00 sec)

獲取星期數的函數WKKE(d)、WEEKOFYEAR(d)

mysql> SELECT WEEK('2017-08-01'),WEEK('2017-08-01',0),WEEK('2017-08-01',1);
+--------------------+----------------------+----------------------+
| WEEK('2017-08-01') | WEEK('2017-08-01',0) | WEEK('2017-08-01',1) |
+--------------------+----------------------+----------------------+
|                 31 |                   31 |                   31 |
+--------------------+----------------------+----------------------+
1 row in set (0.00 sec)

mysql> SELECT WEEK('2017-08-01'),WEEKOFYEAR('2017-08-01');
+--------------------+--------------------------+
| WEEK('2017-08-01') | WEEKOFYEAR('2017-08-01') |
+--------------------+--------------------------+
|                 31 |                       31 |
+--------------------+--------------------------+
1 row in set (0.00 sec)

獲取天數的函數DAYOFYEAR(d)、DAYOFMONTH(d)

mysql> SELECT DAYOFYEAR('2017-08-01');
+-------------------------+
| DAYOFYEAR('2017-08-01') |
+-------------------------+
|                     213 |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT DAYOFMONTH('2017-08-01');
+--------------------------+
| DAYOFMONTH('2017-08-01') |
+--------------------------+
|                        1 |
+--------------------------+
1 row in set (0.00 sec)

獲取年份、季度、小時、分鐘、秒鐘的函數

mysql> SELECT YEAR('11-02-03'),YEAR('96-02-03');
+------------------+------------------+
| YEAR('11-02-03') | YEAR('96-02-03') |
+------------------+------------------+
|             2011 |             1996 |
+------------------+------------------+
1 row in set (0.00 sec)

mysql> SELECT QUARTER('11-02-03'),QUARTER('96-02-03');
+---------------------+---------------------+
| QUARTER('11-02-03') | QUARTER('96-02-03') |
+---------------------+---------------------+
|                   1 |                   1 |
+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> SELECT HOUR('10:05:06'),HOUR('15:06:56');    
+------------------+------------------+
| HOUR('10:05:06') | HOUR('15:06:56') |
+------------------+------------------+
|               10 |               15 |
+------------------+------------------+
1 row in set (0.00 sec)

mysql> SELECT MINUTE('10:05:06'),MINUTE('15:06:56');
+--------------------+--------------------+
| MINUTE('10:05:06') | MINUTE('15:06:56') |
+--------------------+--------------------+
|                  5 |                  6 |
+--------------------+--------------------+
1 row in set (0.00 sec)

mysql> SELECT SECOND('10:05:06'),SECOND('15:06:56');
+--------------------+--------------------+
| SECOND('10:05:06') | SECOND('15:06:56') |
+--------------------+--------------------+
|                  6 |                 56 |
+--------------------+--------------------+
1 row in set (0.00 sec)

獲取日期的指定值的函數EXTRACT(type FROM date)

mysql> SELECT EXTRACT(YEAR FROM '2017-07-03') AS col1, 
    -> EXTRACT(YEAR_MONTH FROM '2017-07-0301:02:03') AS col2, 
    ->EXTRACT(DAY_MINUTE FROM '2017-07-03 01:02:03') AS col3;    
+------+--------+-------+
| col1 | col2   | col3  |
+------+--------+-------+
| 2017 | 201707 | 30102 |
+------+--------+-------+
1 row in set (0.00 sec)

時間和秒鐘轉換的函數

mysql> SELECT TIME_TO_SEC('23:23:00');
+-------------------------+
| TIME_TO_SEC('23:23:00') |
+-------------------------+
|                   84180 |
+-------------------------+
1 row in set (0.00 sec)

mysql> SELECT SEC_TO_TIME(84180);     
+--------------------+
| SEC_TO_TIME(84180) |
+--------------------+
| 23:23:00           |
+--------------------+
1 row in set (0.00 sec)

將日期格式化的函數DATE_FORMAT(date,formate)

說明符說明
%a工作日的縮寫名稱(Sun..Sat)
%b
月份的所寫名稱(Jan..Dec)
%c
月份數字形式(1,..12)
%D
帶有英文后綴的該月份日期(1th,2nd,3rd,..)
%d
該月日期數字形式(01..31)
%e
該月日期數字形式(1,..31)
%f
微秒(000000..999999)
%H
以2位數表示24小時(00,..23)
%h,%I
以2位數表示12小時(01,..12)
%i
分鐘數字形式(00..59)
%j
一年中的天數(001..366)
%k
以24小時表示時間
%l
以12小時表示時間
%M
月份名稱(January..December)
%m
月份數字形式(01..12)
%p
上午AM或下午PM
%r
時間12小時制(小時hh:分鐘mm:秒數ss 后加AM或PM)
%S,%s
以2位數形式表示秒(00..59)
%T
時間24小時制(小時hh:分鐘mm:秒數ss)
%U
周(00..53)周日為每周的第一天
%u
周(00..53)周一為每周的第一天
%V
周(01..53)周日為每周的第一天和%X同時使用
%v
周(01..53)周一為每周的第一天和%x同時使用
%W
工作日名稱(周日..周六)
%w
一周中的每日(0=周日..6=周六)
%X
該周的年份周日為每周第一天數字形式4位數和%V同時使用
%x
該周的年份周一為每周第一天數字形式4位數和%v同時使用
%Y
4位數形式表示年份
%y
2位數形式表示年份
%%
'%'文字字符
mysql> SELECT DATE_FORMAT('1997-10-04 22:23:00','%W %M %Y') AS col1, 
    -> DATE_FORMAT('1997-10-04 22:23:00','%D %y %a %d %m %b %j') AS col2;  
+-----------------------+--------------------------+
| col1                  | col2                     |
+-----------------------+--------------------------+
| Saturday October 1997 | 4th 97 Sat 04 10 Oct 277 |
+-----------------------+--------------------------+
1 row in set (0.00 sec)

將時間格式化的函數TIME_FORMAT(time,formate)

mysql> SELECT TIME_FORMAT('16:00:00','%H %k %h %I %l' );
+-------------------------------------------+
| TIME_FORMAT('16:00:00','%H %k %h %I %l' ) |
+-------------------------------------------+
| 16 16 04 04 4                             |
+-------------------------------------------+
1 row in set (0.00 sec)


5.4、條件判斷函數

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.00 sec)

IFNULL(v1,v2)

mysql> SELECT IFNULL(1,2),IFNULL(NULL,10),IFNULL(1/0,'WRONG');
+-------------+-----------------+---------------------+
| IFNULL(1,2) | IFNULL(NULL,10) | IFNULL(1/0,'WRONG') |
+-------------+-----------------+---------------------+
|           1 |              10 | WRONG               |
+-------------+-----------------+---------------------+
1 row in set (0.00 sec)


5.5、系統信息函數

獲取MySQL版本號VERSION()

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

獲取連接數CONNECTION_ID()

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

顯示運行的線程

mysql> SHOW PROCESSLIST; #顯示運行的線程
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  6 | root | localhost | NULL | Query   |    0 | NULL  | SHOW PROCESSLIST |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

獲取用戶名

mysql> SELECT USER(),CURRENT_USER(),SYSTEM_USER();
+----------------+----------------+----------------+
| USER()         | CURRENT_USER() | SYSTEM_USER()  |
+----------------+----------------+----------------+
| root@localhost | root@localhost | root@localhost |
+----------------+----------------+----------------+
1 row in set (0.00 sec)

獲取字符串的字符集和排序方式

mysql> SELECT CHARSET('ABC'), CHARSET(CONVERT('ABC' USING latin1)), CHARSET(VERSION());    
+----------------+--------------------------------------+--------------------+
| CHARSET('ABC') | CHARSET(CONVERT('ABC' USING latin1)) | CHARSET(VERSION()) |
+----------------+--------------------------------------+--------------------+
| utf8           | latin1                               | utf8               |
+----------------+--------------------------------------+--------------------+
1 row in set (0.00 sec)

mysql> SELECT COLLATION('ABC'),COLLATION(CONVERT('ABC' USING utf8));
+------------------+--------------------------------------+
| COLLATION('ABC') | COLLATION(CONVERT('ABC' USING utf8)) |
+------------------+--------------------------------------+
| utf8_general_ci  | utf8_general_ci                      |
+------------------+--------------------------------------+
1 row in set (0.00 sec)


5.6、加密函數

加密函數PASSWORD(str)

mysql> SELECT PASSWORD('NEWPD');
+-------------------------------------------+
| PASSWORD('NEWPD')                         |
+-------------------------------------------+
| *2AC78BA05A00714DDD77D040F46ABF58440382F2 |
+-------------------------------------------+
1 row in set (0.00 sec)

加密函數MD5(str)

mysql> SELECT MD5('NEWPD');     
+----------------------------------+
| MD5('NEWPD')                     |
+----------------------------------+
| 825ea75e25db1b886e20b14281447628 |
+----------------------------------+
1 row in set (0.00 sec)

加密函數ENCODE(str,pswd_str)

mysql> SELECT ENCODE('secret','cry'),LENGTH(ENCODE('secret','cry'));
+------------------------+--------------------------------+
| ENCODE('secret','cry') | LENGTH(ENCODE('secret','cry')) |
+------------------------+--------------------------------+
|                   |                              6 |
+------------------------+--------------------------------+
1 row in set (0.00 sec)

解密函數DECODE(crypt_str,pswd_str)

mysql> SELECT DECODE(ENCODE('secret','cry'),'cry');                 
+--------------------------------------+
| DECODE(ENCODE('secret','cry'),'cry') |
+--------------------------------------+
| secret                               |
+--------------------------------------+
1 row in set (0.00 sec)

5.7、其他函數

格式化函數FORMAT(x,n)

mysql> SELECT FORMAT(12332.12345,4),FORMAT(12332.1,4); 
+-----------------------+-------------------+
| FORMAT(12332.12345,4) | FORMAT(12332.1,4) |
+-----------------------+-------------------+
| 12,332.1235           | 12,332.1000       |
+-----------------------+-------------------+
1 row in set (0.00 sec)

不同進制的數字進行轉換的函數

mysql> SELECT CONV('a',16,2),
    -> CONV(15,10,2), 
    -> CONV(15,10,8),  
    -> CONV(15,10,16);  
+----------------+---------------+---------------+----------------+
| CONV('a',16,2) | CONV(15,10,2) | CONV(15,10,8) | CONV(15,10,16) |
+----------------+---------------+---------------+----------------+
| 1010           | 1111          | 17            | F              |
+----------------+---------------+---------------+----------------+
1 row in set (0.00 sec)

IP地址與數字相互轉換的函數

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

mysql> SELECT INET_NTOA(3520064480);      
+-----------------------+
| INET_NTOA(3520064480) |
+-----------------------+
| 209.207.235.224       |
+-----------------------+
1 row in set (0.00 sec)

重復執行指定操作的函數

mysql> SELECT BENCHMARK(5000,PASSWORD('NEWPD'));
+-----------------------------------+
| BENCHMARK(5000,PASSWORD('NEWPD')) |
+-----------------------------------+
|                                 0 |
+-----------------------------------+
1 row in set (0.01 sec)

改變字符集的函數

mysql> SELECT CHARSET('STRING'),CHARSET(CONVERT('STRING' USING latin1));
+-------------------+-----------------------------------------+
| CHARSET('STRING') | CHARSET(CONVERT('STRING' USING latin1)) |
+-------------------+-----------------------------------------+
| utf8              | latin1                                  |
+-------------------+-----------------------------------------+
1 row in set (0.00 sec)

改變數據類型的函數

mysql> SELECT CAST(100 AS CHAR(2)),CONVERT('2010-10-01 12:12:12',TIME);
+----------------------+-------------------------------------+
| CAST(100 AS CHAR(2)) | CONVERT('2010-10-01 12:12:12',TIME) |
+----------------------+-------------------------------------+
| 10                   | 12:12:12                            |
+----------------------+-------------------------------------+
1 row in set, 1 warning (0.00 sec)


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

崇州市| 正镶白旗| 商洛市| 安丘市| 饶河县| 扶余县| 湛江市| 宜宾县| 吉安县| 通化市| 温宿县| 遂溪县| 客服| 什邡市| 曲沃县| 罗甸县| 奉贤区| 陇川县| 安吉县| 大方县| 崇礼县| 古蔺县| 阿坝县| 那坡县| 涞源县| 凤城市| 邳州市| 漳平市| 夏津县| 固镇县| 依兰县| 含山县| 乌拉特前旗| 新巴尔虎右旗| 应城市| 仁寿县| 英山县| 云霄县| 客服| 西林县| 漳平市|