MySQL
函數(shù)使用
這里介紹下铐望,MySQL中常用的函數(shù),函數(shù)有太多太多栖袋,不一定都需要記住蚕甥,只需要有個印象,需要的時候去文檔中找一下栋荸,記住一些常用的就好了
數(shù)學(xué)函數(shù)
abs(x)
返回x的絕對值
select abs(-10),abs(10)
ceil(x)菇怀、ceiling(x)
向上取整,比該值大的第一個整數(shù)
select CEIL(9.3),CEIL(9.5),CEIL(9.6),CEIL(-9.5),CEIL(-9.1)
floor(x)
向下取整,比該值小的第一個整數(shù)
select FLOOR(9.3),FLOOR(9.5),FLOOR(9.6),FLOOR(-9.5),FLOOR(-9.1)
round(x),round(x,y)
四舍五入晌块,round(x)爱沟,最近的一個整數(shù),round(x,y)匆背,這個y可以指定精度
select ROUND(9.3),ROUND(9.6),ROUND(9.378,2),ROUND(9.455,2)
rand(),rand(x)
rand() 返回0~1之間的隨機(jī)數(shù)
rand(x) 返回0~1之間的隨機(jī)數(shù)呼伸,如果x值相等,則返回值相等
select RAND(),RAND(),RAND(10),RAND(10)
字符串函數(shù)
獲取字符串長度
length(str),返回str的長度,這里要注意下中文括享,占3個長度,這里的長度單位是bytes
mysql> select length('abc'),length('中國'),length('hi中國');
+---------------+----------------+------------------+
| length('abc') | length('中國') | length('hi中國') |
+---------------+----------------+------------------+
| 3 | 6 | 8 |
+---------------+----------------+------------------+
1 row in set
CHAR_LENGTH(str)搂根,返回str的長度,中文和英文一樣铃辖,占1個字符剩愧,這里長度單位是字符
mysql> select char_length('abc'),char_length('中國'),char_length('hi中國');
+--------------------+---------------------+-----------------------+
| char_length('abc') | char_length('中國') | char_length('hi中國') |
+--------------------+---------------------+-----------------------+
| 3 | 2 | 4 |
+--------------------+---------------------+-----------------------+
1 row in set
字符串拼接
CONCAT(str1,str2,...),將str1娇斩,str2拼接在一起
mysql> select concat('h','e','gogo','中國');
+-------------------------------+
| concat('h','e','gogo','中國') |
+-------------------------------+
| hegogo中國 |
+-------------------------------+
1 row in set
這里要注意下NULL仁卷,如果其中有參數(shù)為NULL,則結(jié)果為NULL
mysql> select concat('h','e','gogo',NULL,'中國');
+------------------------------------+
| concat('h','e','gogo',NULL,'中國') |
+------------------------------------+
| NULL |
+------------------------------------+
1 row in set
CONCAT_WS(separator,str1,str2,...)犬第,使用指定的separator進(jìn)行拼接
mysql> select concat_ws('^','e','gogo',NULL,'中國');
+---------------------------------------+
| concat_ws('^','e','gogo',NULL,'中國') |
+---------------------------------------+
| e^gogo^中國 |
+---------------------------------------+
1 row in set
這里是如果有NULL锦积,對結(jié)果是沒有影響的,會直接忽略NULL值
剔除空格或指定字符
剔除字符串左右的空格
ltrim(str),剔除左側(cè)空格
rtrim(str),剔除右側(cè)空格
TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)
trim可以使用參數(shù)來控制剔除空格或者是指定的remstr歉嗓,默認(rèn)是空格
mysql> select concat(ltrim(' hi '),'oo'),concat(rtrim(' hi '),'oo'),concat(trim(' hi '),'oo');
+--------------------------------+--------------------------------+-------------------------------+
| concat(ltrim(' hi '),'oo') | concat(rtrim(' hi '),'oo') | concat(trim(' hi '),'oo') |
+--------------------------------+--------------------------------+-------------------------------+
| hi oo | hioo | hioo |
+--------------------------------+--------------------------------+-------------------------------+
1 row in set
mysql> select trim('a' from 'aabbbccaa'),trim(leading 'a' from 'aabbbccaa'),trim(trailing 'a' from 'aabbbccaa');
+----------------------------+------------------------------------+-------------------------------------+
| trim('a' from 'aabbbccaa') | trim(leading 'a' from 'aabbbccaa') | trim(trailing 'a' from 'aabbbccaa') |
+----------------------------+------------------------------------+-------------------------------------+
| bbbcc | bbbccaa | aabbbcc |
+----------------------------+------------------------------------+-------------------------------------+
1 row in set
字符串填充
LPAD(str,len,padstr)丰介,左側(cè)填充
RPAD(str,len,padstr),右側(cè)填充
len是指定str的長度鉴分,如果不夠基矮,則使用padstr填充,如果超了冠场,則進(jìn)行截取
mysql> select lpad('hi',6,'@'),lpad('higogo',4,'@'),rpad('hi',6,'@'),rpad('higogo',4,'@');
+------------------+----------------------+------------------+----------------------+
| lpad('hi',6,'@') | lpad('higogo',4,'@') | rpad('hi',6,'@') | rpad('higogo',4,'@') |
+------------------+----------------------+------------------+----------------------+
| @@@@hi | higo | hi@@@@ | higo |
+------------------+----------------------+------------------+----------------------+
1 row in set
字符串截取
LEFT(str,len)家浇,從左側(cè)開始截取len個字符
RIGHT(str,len),從右側(cè)截取len個字符
SUBSTR(str,pos), SUBSTR(str FROM pos), SUBSTR(str,pos,len), SUBSTR(str FROM pos FOR len)碴裙,從指定pos開始截取len個字符
mysql> select left('hello',1),left('hello',3),right('hello',3);
+-----------------+-----------------+------------------+
| left('hello',1) | left('hello',3) | right('hello',3) |
+-----------------+-----------------+------------------+
| h | hel | llo |
+-----------------+-----------------+------------------+
1 row in set
mysql> select substr('hello',1),substr('hello',2),substr('hello' from 2);
+-------------------+-------------------+------------------------+
| substr('hello',1) | substr('hello',2) | substr('hello' from 2) |
+-------------------+-------------------+------------------------+
| hello | ello | ello |
+-------------------+-------------------+------------------------+
1 row in set
mysql> select substr('hello',1,3),substr('hello',2,3),substr('hello' from 2 for 2);
+---------------------+---------------------+------------------------------+
| substr('hello',1,3) | substr('hello',2,3) | substr('hello' from 2 for 2) |
+---------------------+---------------------+------------------------------+
| hel | ell | el |
+---------------------+---------------------+------------------------------+
1 row in set
大小寫轉(zhuǎn)換
LOWER(str) LCASE(str)钢悲,將str轉(zhuǎn)為小寫
UPPER(str) UCASE(str),將str轉(zhuǎn)為大寫
mysql> select lower('AppLE'),lcase('AppLE'),upper('AppLE'),ucase('AppLE');
+----------------+----------------+----------------+----------------+
| lower('AppLE') | lcase('AppLE') | upper('AppLE') | ucase('AppLE') |
+----------------+----------------+----------------+----------------+
| apple | apple | APPLE | APPLE |
+----------------+----------------+----------------+----------------+
1 row in set
更多字符串函數(shù)
參考官網(wǎng): https://dev.mysql.com/doc/refman/5.7/en/string-functions.html
日期和函數(shù)
獲取當(dāng)前日期舔株、時間
SELECT CURRENT_DATE,CURRENT_DATE(),CURRENT_TIME,CURRENT_TIME(),CURRENT_TIMESTAMP(),NOW()
時間戳相關(guān)函數(shù)
UNIX_TIMESTAMP() 返回當(dāng)前時間的時間戳,
UNIX_TIMESTAMP(x) 返回指定日期的時間戳
FROM_UNIXTIME(x) 將時間戳轉(zhuǎn)為日期
FROM_UNIXTIME(x,y) 將時間戳轉(zhuǎn)為指定格式的日期
mysql> select UNIX_TIMESTAMP(),UNIX_TIMESTAMP('2017-09-10');
+------------------+------------------------------+
| UNIX_TIMESTAMP() | UNIX_TIMESTAMP('2017-09-10') |
+------------------+------------------------------+
| 1505096065 | 1504972800 |
+------------------+------------------------------+
1 row in set
mysql> select FROM_UNIXTIME(1505096033),FROM_UNIXTIME(1504972800),FROM_UNIXTIME(1505096033,'%Y-%m-%d');
+---------------------------+---------------------------+--------------------------------------+
| FROM_UNIXTIME(1505096033) | FROM_UNIXTIME(1504972800) | FROM_UNIXTIME(1505096033,'%Y-%m-%d') |
+---------------------------+---------------------------+--------------------------------------+
| 2017-09-11 10:13:53 | 2017-09-10 00:00:00 | 2017-09-11 |
+---------------------------+---------------------------+--------------------------------------+
1 row in set
mysql>
extract
EXTRACT(unit FROM date)
返回日期/時間的單獨部分载慈,比如年惭等、月、日办铡、小時辞做、分鐘等等
date 參數(shù)是合法的日期表達(dá)式。unit 參數(shù)可以是下列的值:
mysql> select extract(YEAR FROM NOW()),extract(MONTH from now()),extract(HOUR from now());
+--------------------------+---------------------------+--------------------------+
| extract(YEAR FROM NOW()) | extract(MONTH from now()) | extract(HOUR from now()) |
+--------------------------+---------------------------+--------------------------+
| 2017 | 9 | 10 |
+--------------------------+---------------------------+--------------------------+
1 row in set
mysql>
datediff寡具、timediff秤茅、timestampdiff
datediff(expr1,expr2),獲取2個日期相差的天數(shù)
mysql> select datediff('2017-09-11 10:00:00','2017-09-08 00:00:00');
+-------------------------------------------------------+
| datediff('2017-09-11 10:00:00','2017-09-08 00:00:00') |
+-------------------------------------------------------+
| 3 |
+-------------------------------------------------------+
1 row in set
mysql>
TIMEDIFF(expr1,expr2),返回expr1-expr2的時間差
mysql> select timediff('2017-09-11 10:00:00','2017-09-08 00:00:00');
+-------------------------------------------------------+
| timediff('2017-09-11 10:00:00','2017-09-08 00:00:00') |
+-------------------------------------------------------+
| 82:00:00 |
+-------------------------------------------------------+
1 row in set
mysql>
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
返回指定unit的datetime_expr2 ? datetime_expr1時間差
unit可以是MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.
mysql> select timestampdiff(DAY,'2017-09-11 10:00:00','2017-09-08 00:00:00');
+----------------------------------------------------------------+
| timestampdiff(DAY,'2017-09-11 10:00:00','2017-09-08 00:00:00') |
+----------------------------------------------------------------+
| -3 |
+----------------------------------------------------------------+
1 row in set
mysql> select timestampdiff(HOUR,'2017-09-11 10:00:00','2017-09-08 00:00:00');
+-----------------------------------------------------------------+
| timestampdiff(HOUR,'2017-09-11 10:00:00','2017-09-08 00:00:00') |
+-----------------------------------------------------------------+
| -82 |
+-----------------------------------------------------------------+
1 row in set
時間加減函數(shù)
對日期進(jìn)行加減操作童叠,有很多方法可以使用框喳,最簡單的是直接使用interval
mysql> select now(),now() + interval 3 DAY,now()+interval 1 Hour;
+---------------------+------------------------+-----------------------+
| now() | now() + interval 3 DAY | now()+interval 1 Hour |
+---------------------+------------------------+-----------------------+
| 2017-09-11 10:57:18 | 2017-09-14 10:57:18 | 2017-09-11 11:57:18 |
+---------------------+------------------------+-----------------------+
1 row in set
當(dāng)然也可使用提供的函數(shù)
ADDDATE(date,INTERVAL expr unit), ADDDATE(expr,days)
DATE_ADD(date,INTERVAL expr unit), DATE_SUB(date,INTERVAL expr unit)
mysql> select now(),date_add(now(),interval 1 Day),adddate(now(),interval 3 Hour);
+---------------------+--------------------------------+--------------------------------+
| now() | date_add(now(),interval 1 Day) | adddate(now(),interval 3 Hour) |
+---------------------+--------------------------------+--------------------------------+
| 2017-09-11 10:59:09 | 2017-09-12 10:59:09 | 2017-09-11 13:59:09 |
+---------------------+--------------------------------+--------------------------------+
1 row in set
更多日期、時間函數(shù)
參考官方介紹: [https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html](https:
//dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html)
條件判斷函數(shù)
if
if(expr,v1,v2)
如果expr為真,則返回v1五垮,為假升筏,則返回v2
mysql> select if(1>0,'ok','no'),if(1=0,'ok','no');
+-------------------+-------------------+
| if(1>0,'ok','no') | if(1=0,'ok','no') |
+-------------------+-------------------+
| ok | no |
+-------------------+-------------------+
1 row in set
mysql>
ifnull
ifnull(v1,v2)
如果v1的值為null颊乘,則返回v2林螃,如果v1不為null抢蚀,則返回v1
mysql> select ifnull(99,20),ifnull(NULL,99);
+---------------+-----------------+
| ifnull(99,20) | ifnull(NULL,99) |
+---------------+-----------------+
| 99 | 99 |
+---------------+-----------------+
1 row in set
mysql>
case when
這里可以根據(jù)多個條件來判斷峻凫,在不同的情況下秋泄,返回不同的值
select
s_id,s_name,s_gender,
case
when s_gender=0 then '男'
when s_gender=1 then '女'
end gender,
s_birthday,
s_hobby,
c_id
from
t_student
;