您好,登錄后才能下訂單哦!
MySQL數(shù)據(jù)庫中的函數(shù)有哪些?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
前言:
MySQL數(shù)據(jù)庫中提供了很豐富的函數(shù),比如我們常用的聚合函數(shù),日期及字符串處理函數(shù)等。SELECT語句及其條件表達(dá)式都可以使用這些函數(shù),函數(shù)可以幫助用戶更加方便的處理表中的數(shù)據(jù),使MySQL數(shù)據(jù)庫的功能更加強(qiáng)大。本篇文章主要為大家介紹幾類常用函數(shù)的用法。
1.聚合函數(shù)
聚合函數(shù)是平時比較常用的一類函數(shù),這里列舉如下:
2.數(shù)值型函數(shù)
數(shù)值型函數(shù)主要是對數(shù)值型數(shù)據(jù)進(jìn)行處理,得到我們想要的結(jié)果,常用的幾個列舉如下,具體使用方法大家可以試試看。
一些示例:
# ABS()函數(shù)求絕對值 mysql> SELECT ABS(5),ABS(-2.4),ABS(-24),ABS(0); +--------+-----------+----------+--------+ | ABS(5) | ABS(-2.4) | ABS(-24) | ABS(0) | +--------+-----------+----------+--------+ | 5 | 2.4 | 24 | 0 | +--------+-----------+----------+--------+ # 取整函數(shù) CEIL(x) 和 CEILING(x) 的意義相同,返回不小于 x 的最小整數(shù)值 mysql> SELECT CEIL(-2.5),CEILING(2.5); +------------+--------------+ | CEIL(-2.5) | CEILING(2.5) | +------------+--------------+ | -2 | 3 | +------------+--------------+ # 求余函數(shù) MOD(x,y) 返回 x 被 y 除后的余數(shù) mysql> SELECT MOD(63,8),MOD(120,10),MOD(15.5,3); +-----------+-------------+-------------+ | MOD(63,8) | MOD(120,10) | MOD(15.5,3) | +-----------+-------------+-------------+ | 7 | 0 | 0.5 | +-----------+-------------+-------------+ # RAND() 函數(shù)被調(diào)用時,可以產(chǎn)生一個在 0 和 1 之間的隨機(jī)數(shù) mysql> SELECT RAND(), RAND(), RAND(); +---------------------+--------------------+----------------------+ | RAND() | RAND() | RAND() | +---------------------+--------------------+----------------------+ | 0.24996517063115273 | 0.9559759106077029 | 0.029984071878701515 | +---------------------+--------------------+----------------------+
3.字符串函數(shù)
字符串函數(shù)可以對字符串類型數(shù)據(jù)進(jìn)行處理,在程序應(yīng)用中用處還是比較大的,同樣這里列舉幾個常用的如下:
一些示例:
# LENGTH(str) 函數(shù)的返回值為字符串的字節(jié)長度 mysql> SELECT LENGTH('name'),LENGTH('數(shù)據(jù)庫'); +----------------+---------------------+ | LENGTH('name') | LENGTH('數(shù)據(jù)庫') | +----------------+---------------------+ | 4 | 9 | +----------------+---------------------+ # CONCAT(sl,s2,...) 函數(shù)返回結(jié)果為連接參數(shù)產(chǎn)生的字符串 若有任何一個參數(shù)為 NULL,則返回值為 NULL mysql> SELECT CONCAT('MySQL','5.7'),CONCAT('MySQL',NULL); +-----------------------+----------------------+ | CONCAT('MySQL','5.7') | CONCAT('MySQL',NULL) | +-----------------------+----------------------+ | MySQL5.7 | NULL | +-----------------------+----------------------+ # INSERT(s1,x,len,s2) 返回字符串 s1,子字符串起始于 x 位置,并且用 len 個字符長的字符串代替 s2 mysql> SELECT INSERT('Football',2,4,'Play') AS col1, -> INSERT('Football',-1,4,'Play') AS col2, -> INSERT('Football',3,20,'Play') AS col3; +----------+----------+--------+ | col1 | col2 | col3 | +----------+----------+--------+ | FPlayall | Football | FoPlay | +----------+----------+--------+ # UPPER,LOWER是大小寫轉(zhuǎn)換函數(shù) mysql> SELECT LOWER('BLUE'),LOWER('Blue'),UPPER('green'),UPPER('Green'); +---------------+---------------+----------------+----------------+ | LOWER('BLUE') | LOWER('Blue') | UPPER('green') | UPPER('Green') | +---------------+---------------+----------------+----------------+ | blue | blue | GREEN | GREEN | +---------------+---------------+----------------+----------------+ # LEFT,RIGHT是截取左邊或右邊字符串函數(shù) mysql> SELECT LEFT('MySQL',2),RIGHT('MySQL',3); +-----------------+------------------+ | LEFT('MySQL',2) | RIGHT('MySQL',3) | +-----------------+------------------+ | My | SQL | +-----------------+------------------+ # REPLACE(s,s1,s2) 使用字符串 s2 替換字符串 s 中所有的字符串 s1 mysql> SELECT REPLACE('aaa.mysql.com','a','w'); +----------------------------------+ | REPLACE('aaa.mysql.com','a','w') | +----------------------------------+ | www.mysql.com | +----------------------------------+ # 函數(shù) SUBSTRING(s,n,len) 帶有 len 參數(shù)的格式,從字符串 s 返回一個長度同 len 字符相同的子字符串,起始于位置 n mysql> SELECT SUBSTRING('computer',3) AS col1, -> SUBSTRING('computer',3,4) AS col2, -> SUBSTRING('computer',-3) AS col3, -> SUBSTRING('computer',-5,3) AS col4; +--------+------+------+------+ | col1 | col2 | col3 | col4 | +--------+------+------+------+ | mputer | mput | ter | put | +--------+------+------+------+
4.日期和時間函數(shù)
一些示例:
# CURDATE() 和 CURRENT_DATE() 函數(shù)的作用相同,將當(dāng)前日期按照“YYYY-MM-DD”或“YYYYMMDD”格式的值返回 mysql> SELECT CURDATE(),CURRENT_DATE(),CURRENT_DATE()+0; +------------+----------------+------------------+ | CURDATE() | CURRENT_DATE() | CURRENT_DATE()+0 | +------------+----------------+------------------+ | 2019-10-22 | 2019-10-22 | 20191022 | +------------+----------------+------------------+ # MONTH(date) 函數(shù)返回指定 date 對應(yīng)的月份 mysql> SELECT MONTH('2017-12-15'); +---------------------+ | MONTH('2017-12-15') | +---------------------+ | 12 | +---------------------+ # DATE_ADD(date,INTERVAL expr type) 和 ADDDATE(date,INTERVAL expr type) 兩個函數(shù)的作用相同,都是用于執(zhí)行日期的加運(yùn)算。 mysql> SELECT DATE_ADD('2018-10-31 23:59:59',INTERVAL 1 SECOND) AS C1, -> DATE_ADD('2018-10-31 23:59:59',INTERVAL '1:1' MINUTE_SECOND) AS C2, -> ADDDATE('2018-10-31 23:59:59',INTERVAL 1 SECOND) AS C3; +---------------------+---------------------+---------------------+ | C1 | C2 | C3 | +---------------------+---------------------+---------------------+ | 2018-11-01 00:00:00 | 2018-11-01 00:01:00 | 2018-11-01 00:00:00 | +---------------------+---------------------+---------------------+ # DATEDIFF(date1,date2) 返回起始時間 date1 和結(jié)束時間 date2 之間的天數(shù) mysql> SELECT DATEDIFF('2017-11-30','2017-11-29') AS COL1, -> DATEDIFF('2017-11-30','2017-12-15') AS col2; +------+------+ | COL1 | col2 | +------+------+ | 1 | -15 | +------+------+ # DATE_FORMAT(date,format) 函數(shù)是根據(jù) format 指定的格式顯示 date 值 mysql> SELECT DATE_FORMAT('2017-11-15 21:45:00','%W %M %D %Y') AS col1, -> DATE_FORMAT('2017-11-15 21:45:00','%h:i% %p %M %D %Y') AS col2; +------------------------------+----------------------------+ | col1 | col2 | +------------------------------+----------------------------+ | Wednesday November 15th 2017 | 09:i PM November 15th 2017 | +------------------------------+----------------------------+
5.流程控制函數(shù)
流程控制類函數(shù)可以進(jìn)行條件操作,用來實(shí)現(xiàn)SQL的條件邏輯,允許開發(fā)者將一些應(yīng)用程序業(yè)務(wù)邏輯轉(zhuǎn)換到數(shù)據(jù)庫后臺,列舉如下:
一些示例:
# IF 語句允許根據(jù)表達(dá)式的某個條件或值結(jié)果來執(zhí)行一組 SQL 語句 mysql> SELECT IF(1<2,1,0) c1,IF(1>5,'√','×') c2,IF(STRCMP('abc','ab'),'yes','no') c3; +----+----+-----+ | c1 | c2 | c3 | +----+----+-----+ | 1 | × | yes | +----+----+-----+ # IFNULL接受兩個參數(shù),如果第一個不是 NULL,則返回第一個參數(shù)。否則,IFNULL 函數(shù)返回第二個參數(shù) mysql> SELECT IFNULL(5,8),IFNULL(NULL,'OK'); +-------------+-------------------+ | IFNULL(5,8) | IFNULL(NULL,'OK') | +-------------+-------------------+ | 5 | OK | +-------------+-------------------+ # NULLIF函數(shù) mysql> SELECT NULLIF(5,8),NULLIF(8,8); +-------------+-------------+ | NULLIF(5,8) | NULLIF(8,8) | +-------------+-------------+ | 5 | NULL | +-------------+-------------+ # CASE WHEN 函數(shù) mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END; +--------------------------------------------+ | CASE WHEN 1>0 THEN 'true' ELSE 'false' END | +--------------------------------------------+ | true | +--------------------------------------------+ mysql> SELECT CASE 11 WHEN 1 THEN 'one' -> WHEN 2 THEN 'two' ELSE 'more' END; +-------------------------------------------------------------+ | CASE 11 WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'more' END | +-------------------------------------------------------------+ | more | +-------------------------------------------------------------+
6.加密函數(shù)
加密函數(shù)主要用于對字符串進(jìn)行加密,常用的幾個列舉如下:
一些示例:
# ENCRYPT函數(shù) mysql> SELECT ENCRYPT('root','salt'); +------------------------+ | ENCRYPT('root','salt') | +------------------------+ | saFKJij3eLACw | +------------------------+ # MD5()函數(shù) mysql> SELECT MD5('123456'); +----------------------------------+ | MD5('123456') | +----------------------------------+ | e10adc3949ba59abbe56e057f20f883e | +----------------------------------+ # PASSWORD函數(shù) mysql> select PASSWORD('abcd'); +-------------------------------------------+ | PASSWORD('abcd') | +-------------------------------------------+ | *A154C52565E9E7F94BFC08A1FE702624ED8EFFDA | +-------------------------------------------+
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。