MySQL常用函數匯總

來源:互聯網
上載者:User

MySQL常用函數匯總

一、數學函數

1.絕對值函數abs(value)

 mysql> select abs(-120);
+-----------+
| abs(-120) |
+-----------+
|      120 |
+-----------+
1 row in set (0.00 sec)

2.地板函數floor(value),擷取小於value的最大整數

mysql> select floor(23.8);
+-------------+
| floor(23.8) |
+-------------+
|          23 |
+-------------+
1 row in set (0.00 sec)

3.天花板函數ceiling(value),擷取大於value的最小整數

mysql> select ceiling(23.8);
+---------------+
| ceiling(23.8) |
+---------------+
|            24 |
+---------------+
1 row in set (0.00 sec)

4.四捨五入函數round(value,position),四捨五入到小數點後幾位

mysql> select round(23.27); --預設是0
+--------------+
| round(23.27) |
+--------------+
|          23 |
+--------------+
1 row in set (0.00 sec)

mysql> select round(23.27,1);
+----------------+
| round(23.27,1) |
+----------------+
|          23.3 |
+----------------+
1 row in set (0.00 sec)

5.隨機數函數rand(),擷取0到1之間隨機數

mysql> select rand();
+---------------------+
| rand()              |
+---------------------+
| 0.12718137365503365 |
+---------------------+
1 row in set (0.00 sec)

二、字串函數

1.串連函數concat(str1,str2)

mysql> select concat('hello','world');
+-------------------------+
| concat('hello','world') |
+-------------------------+
| helloworld              |
+-------------------------+
1 row in set (0.00 sec)

2.帶分隔字元的串連函數concat_ws(separator,str1,str2)

mysql> select concat_ws('#','hello','world');
+--------------------------------+
| concat_ws('#','hello','world') |
+--------------------------------+
| hello#world                    |
+--------------------------------+
1 row in set (0.00 sec)

3.字串所佔位元組數length(str)

mysql> select length('helloworld我');
+------------------------+
| length('helloworld我')  |
+------------------------+
|                    12 |
+------------------------+
1 row in set (0.00 sec)

4.字串的字元個數char_length(str)

mysql> select char_length('helloworld我');
+-----------------------------+
| char_length('helloworld我')  |
+-----------------------------+
|                          12 |
+-----------------------------+
1 row in set (0.00 sec)

5.字串判斷函數

a. IF(exp1,exp2,exp3):若是exp1 為真,返回exp2;若是exp1為假,返回exp3

mysql> select if(2>1,'hello','world');
+-------------------------+
| if(2>1,'hello','world') |
+-------------------------+
| hello                  |
+-------------------------+
1 row in set (0.00 sec)

mysql> select if(null=1,'hello','world');
+----------------------------+
| if(null=1,'hello','world') |
+----------------------------+
| world                      |
+----------------------------+
1 row in set (0.08 sec)

b. IFNULL(exp1,exp2):若是exp1 IS NOT NULL,返回exp1,否則返回exp2

 mysql> select IFNULL('hello','world');
+-------------------------+
| IFNULL('hello','world') |
+-------------------------+
| hello                  |
+-------------------------+
1 row in set (0.00 sec)

mysql> select IFNULL(null,'world');
+----------------------+
| IFNULL(null,'world') |
+----------------------+
| world                |
+----------------------+
1 row in set (0.06 sec)

c. NULLIF(exp1,exp2):若是exp1=exp2,返回NULL,否則返回exp1

mysql> select NULLIF('hello','hello');
+-------------------------+
| NULLIF('hello','hello') |
+-------------------------+
| NULL                    |
+-------------------------+
1 row in set (0.00 sec)

mysql> select NULLIF('hello','hello11');
+---------------------------+
| NULLIF('hello','hello11') |
+---------------------------+
| hello                    |
+---------------------------+
1 row in set (0.00 sec)

6. 字串轉換函式

a. LTRIM(exp1):去掉exp1中字串開頭(LEFT)的空格

mysql> select LTRIM('  HELLO');
+------------------+
| LTRIM('  HELLO') |
+------------------+
| HELLO            |
+------------------+
1 row in set (0.00 sec)

b.RTRIM(exp1):去掉exp1中字串結尾(RIGHT)的空格

mysql> select LTRIM('  HELLO      ');
+--------------------------+
| LTRIM('  HELLO      ') |
+--------------------------+
| HELLO                    |
+--------------------------+
1 row in set (0.00 sec)

c.TRIM(exp1):去掉exp1中的開頭和 結尾的空格

mysql> select TRIM('  HELLO      ');
+-------------------------+
| TRIM('  HELLO      ') |
+-------------------------+
| HELLO                  |
+-------------------------+
1 row in set (0.00 sec)

7.字串尋找函數

a.SUBSTRING_INDEX(exp1,delim,count),其中exp1為字串,delim為分割符號,count表示第幾個風格符號

mysql> select substring_index('1121.qq..com','.',1);
+---------------------------------------+
| substring_index('1121.qq..com','.',1) |
+---------------------------------------+
| 1121                                  |
+---------------------------------------+
1 row in set (0.00 sec)

b.  SUBSTRING(exp1,pos,len): exp1為字串,pos為位置,len為長度

mysql> select substring('1121.qq.com',1,9);
+------------------------------+
| substring('1121.qq.com',1,9) |
+------------------------------+
| 1121.qq.c                    |
+----------------------------+
1 row in set (0.00 sec)

8.日期操作函數
日期格式轉換函式
將字串轉換成日期方式,DATE_FORMAT() 或STR_TO_DATE(),兩個函數的格式如下
DATE_FORMAT(expr1,format)
STR_TO_DATE(expr1, format)a
常用的日期格式YYYY-MM-DD HH:MM:SS 對應的format為%Y-%m-%d %H:%i:%S
日期運算函數
CURDATE():返回當天的日期,格式: YYYY-MM-DD
NOW():返回當下的日期時間,格式: YYYY-MM-DD HH:MM:SS
DATE_ADD(date,INTERVAL expr unit):expr 運算式,unit為單位,

例如:
DATE_ADD(‘2013-08-20’,INTERVAL -1 DAY),返回:2013-08-19

9.日期運算函數
MONTH(date):返回date的月份數,例如MONTY(‘2013-08-20’),返回:8
DAY(date):返回date的天數,例如DAY(‘2013-08-20’),返回:20
YEAR(date):返回date的年份數,例如YEAR(‘2013-08-20’),返回:2013
TO_DAYS(date):date對應的天數,例如TO_DAYS (‘2008-10-07’),返回:733687
WEEK(date):返回date對應周幾,例如: WEEK(‘2013-08-20’),返回:2
DATEDIFF(date1,date2):返回date1-date2的天數差別,例如:
DATEDIFF(‘2013-08-20’,’2013-08-18 13:30:14’),返回:2

10.類型轉換函式
CAST(expr  AS  type):expr轉換成某類型
CONVERT(expr , type) : expr轉換成某類型
CONVERT(expr  USING  transcoding_name):expr字元集轉換成某字元集

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.