明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

MySQL中的常用函數(shù)詳細(xì)說(shuō)明

[摘要]在MySQL中,函數(shù)不僅可以出現(xiàn)在select語(yǔ)句及其子句中,而且還可以出現(xiàn)在update、delete語(yǔ)句中。本文主要介紹了MySQL中的常用函數(shù)的相關(guān)資料,需要的朋友可以參考下,希望能幫助到大家...
在MySQL中,函數(shù)不僅可以出現(xiàn)在select語(yǔ)句及其子句中,而且還可以出現(xiàn)在update、delete語(yǔ)句中。本文主要介紹了MySQL中的常用函數(shù)的相關(guān)資料,需要的朋友可以參考下,希望能幫助到大家。

常用的函數(shù)有:

1. 字符串函數(shù);主要用于處理字符串。

2. 數(shù)值函數(shù);主要用于處理數(shù)字。

3. 日期和時(shí)間函數(shù);主要用于處理日期和事件。

4. 系統(tǒng)信息函數(shù);獲取系統(tǒng)信息。

1. 使用字符串函數(shù):

雖然每種數(shù)據(jù)庫(kù)都支持SQL,但是每種數(shù)據(jù)庫(kù)擁有各自所支持的函數(shù)。

1.1 合并字符串函數(shù)concat() 和 concat_ws():

在MySQL中可以通過(guò)函數(shù)concat()和concat_ws()將傳入的參數(shù)連接成為一個(gè)字符串。

語(yǔ)法定義為:

concat(s1, s2,...sn)
//該函數(shù)會(huì)將傳入的參數(shù)連接起來(lái)返回合并的字符串類型的數(shù)據(jù)。如果其中一個(gè)參數(shù)為null,則返回值為null.

示例:

mysql> select concat('my','s','ql');
+-----------------------+
  concat('my','s','ql')  
+-----------------------+
  mysql  
+-----------------------+
1 row in set (0.00 sec)
mysql> select concat('my','s','ql',null);
+----------------------------+
  concat('my','s','ql',null)  
+----------------------------+
  NULL  
+----------------------------+
1 row in set (0.00 sec)
mysql> select concat(curdate(), 12.2);
+-------------------------+
  concat(curdate(), 12.2)  
+-------------------------+
  2016-08-2512.2  
+-------------------------+
1 row in set (0.00 sec)
//說(shuō)明:將當(dāng)前時(shí)間和數(shù)值12.2合并。即concat()函數(shù)不僅可以接受字符串參數(shù),而且還可以接受其他類型參數(shù)。

concat_ws()的定義:

concat_ws(sep,s1,s2,...sn)
//該函數(shù)與concat()相比,多了一個(gè)表示分隔符的seq參數(shù),不僅將傳入的其他參數(shù)連接起來(lái),而且還會(huì)通過(guò)分隔符將各個(gè)字符串分割開(kāi)來(lái)。
//分隔符可以是一個(gè)字符串,也可以是其他參數(shù)。如果分割符為null,則返回結(jié)果為null。函數(shù)會(huì)忽略任何分割符后的參數(shù)null.

示例:

mysql> select concat_ws('-','020','87658907');
+---------------------------------+
  concat_ws('-','020','87658907')  
+---------------------------------+
  020-87658907  
+---------------------------------+
1 row in set (0.00 sec)
mysql> select concat_ws(null,'020','87658907');
+----------------------------------+
  concat_ws(null,'020','87658907')  
+----------------------------------+
  NULL  
+----------------------------------+
1 row in set (0.00 sec)
//當(dāng)分隔符為null時(shí),則返回結(jié)果為null
mysql> select concat_ws('-','020',null,'87658907');
+--------------------------------------+
  concat_ws('-','020',null,'87658907')  
+--------------------------------------+
  020-87658907  
+--------------------------------------+
1 row in set (0.00 sec)
//不是第一個(gè)參數(shù)的null將被忽略

1.2 比較字符串大小函數(shù)strcmp():

strcmp()定義為:

strcmp(str1,str2);
//如果參數(shù)str1大于str2,返回1;如果str1小于str2,則返回-1;如果str1等于str2,則返回0;

示例:

mysql> select strcmp('abc','abd'),strcmp('abc','abc'),strcmp('abc','abb');
+---------------------+---------------------+---------------------+
  strcmp('abc','abd')   strcmp('abc','abc')   strcmp('abc','abb')  
+---------------------+---------------------+---------------------+
  -1   0   1  
+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)

1.3 獲取字符串長(zhǎng)度函數(shù)length()和字符數(shù)函數(shù)char_length():

length()的定義如下:

length(str)

char_length(str)的定義如下:

char_length(str)

示例:

mysql> select length('mysql'),length('漢字'),char_length('mysql'),char_length('漢字');
+-----------------+----------------+----------------------+---------------------+
  length('mysql')   length('漢字')   char_length('mysql')   char_length('漢字')  
+-----------------+----------------+----------------------+---------------------+
  5   4   5   4  
+-----------------+----------------+----------------------+---------------------+
1 row in set, 2 warnings (0.00 sec)
//字符串‘MySQL'共有5個(gè)字符,但是占6個(gè)字節(jié)空間。這是因?yàn)槊總(gè)字符串都是以\0結(jié)束。兩個(gè)函數(shù)都是獲取字符串的字符數(shù)而不是所占空間大小。

1.4 字母的大小寫(xiě)轉(zhuǎn)換upper()和lower():

字母大小轉(zhuǎn)換函數(shù):upper(s); ucase(s);
字母小寫(xiě)轉(zhuǎn)換函數(shù):lower(s); lcase(s);

示例:

mysql> select upper('mysql'),ucase('mYsql'),lower('MYSQL'),lcase('MYsql');
+----------------+----------------+----------------+----------------+
  upper('mysql')   ucase('mYsql')   lower('MYSQL')   lcase('MYsql')  
+----------------+----------------+----------------+----------------+
  MYSQL   MYSQL   mysql   mysql  
+----------------+----------------+----------------+----------------+
1 row in set (0.00 sec)

1.5 查找字符串:

mysql中提供了豐富的函數(shù)去查找字符串的位置。分別有find_in_set()函數(shù)、field()函數(shù)、locate()函數(shù)、position()函數(shù)和instr()函數(shù)。同時(shí)還提供了查找指定位置的字符串的函數(shù)elt()。

1.5.1 返回字符串位置的find_in_set()函數(shù):

函數(shù)定義為:

find_in_set(str1,str2) 
//會(huì)返回在字符串str2中與str1相匹配的字符串的位置,參數(shù)str2字符串中將包含若干個(gè)用逗號(hào)隔開(kāi)的字符串。

示例:

mysql> select find_in_set('mysql','oracle,mysql,db2');
+-----------------------------------------+
  find_in_set('mysql','oracle,mysql,db2')  
+-----------------------------------------+
  2  
+-----------------------------------------+
1 row in set (0.00 sec)

1.5.2 返回指定字符串位置的field()函數(shù):

函數(shù)定義為:

filed(str,str1,str2...)
//返回第一個(gè)與字符串str匹配的字符串的位置。

示例:

mysql> select field('mysql','oracle','db2','redis','mysql');
+-----------------------------------------------+
  field('mysql','oracle','db2','redis','mysql')  
+-----------------------------------------------+
  4  
+-----------------------------------------------+
1 row in set (0.00 sec)

1.5.3 返回子字符串相匹配的開(kāi)始位置:

mysql中有三個(gè)函數(shù)可以獲取子字符串相匹配的開(kāi)始位置,分別是locate()、position()、instr()函數(shù)。

locate(str1,str) //返回參數(shù)str中字符串str1的開(kāi)始位置

position(str1 in str) 和 instr(str,str1)

示例:

mysql> select locate('sql','mysql'),position('sql' in 'mysql'),instr('mysql','sql');
+-----------------------+----------------------------+----------------------+
  locate('sql','mysql')   position('sql' in 'mysql')   instr('mysql','sql')  
+-----------------------+----------------------------+----------------------+
  3   3   3  
+-----------------------+----------------------------+----------------------+
1 row in set (0.00 sec)

1.5.4 返回指定位置的字符串的elt()函數(shù):

函數(shù)語(yǔ)法為:

elt(n,str1,str2...);

示例:

mysql> select elt(1,'mysql','db2','oracle');
+-------------------------------+
  elt(1,'mysql','db2','oracle')  
+-------------------------------+
  mysql  
+-------------------------------+
1 row in set (0.00 sec)

1.5.5 選擇字符串的make_set()函數(shù):

函數(shù)定義為:

make_set(num,str1,str2...strn)

示例:

mysql> select bin(5),make_set(5,'mysql','db2','oracle','redus');
+--------+--------------------------------------------+
  bin(5)   make_set(5,'mysql','db2','oracle','redus')  
+--------+--------------------------------------------+
  101   mysql,oracle  
+--------+--------------------------------------------+
1 row in set (0.00 sec)
//make_set()首先會(huì)將數(shù)值num轉(zhuǎn)換成二進(jìn)制數(shù),然后按照二進(jìn)制從參數(shù)str1,str2,...,strn中選取相應(yīng)的字符串。再通過(guò)二進(jìn)制從右到左的順序讀取該值,如果值為1選擇該字符串,否則將不選擇該字符串。

1.6 從現(xiàn)有字符串中截取子字符串:

截取子字符串的函數(shù)有:left(),right(),substring(),mid();

1.6.1 從左邊或右邊截取子字符串:

函數(shù)定義為:

left(str,num)
//返回字符串str中包含前num個(gè)字母(從左邊數(shù))的字符串。
right(str,num)
//返回字符串str中包含后num個(gè)字母(從右邊數(shù))的字符串。

示例:

mysql> select left('mysql',2),right('mysql',3);
+-----------------+------------------+
  left('mysql',2)   right('mysql',3)  
+-----------------+------------------+
  my   sql  
+-----------------+------------------+
1 row in set (0.00 sec)

1.6.2 截取指定位置和長(zhǎng)度的字符串:

可以通過(guò)substring()和mid()函數(shù)截取指定位置和長(zhǎng)度的字符串。

函數(shù)語(yǔ)法為:

substring(str,num,len) //返回字符串str中的第num個(gè)位置開(kāi)始長(zhǎng)度為len的子字符串。
mid(str,num,len)

示例:

mysql> select substring('zhaojd',2,3),mid('zhaojd',2,4);
+-------------------------+-------------------+
  substring('zhaojd',2,3)   mid('zhaojd',2,4)  
+-------------------------+-------------------+
  hao   haoj  
+-------------------------+-------------------+
1 row in set (0.00 sec)

1.7 去除字符串的首尾空格:

去除字符串首尾空格的函數(shù)有:ltrim()、rtrim()、trim()

1.7.1 去除字符串開(kāi)始處的空格:

函數(shù)定義如下:

ltrim(str) //返回去掉開(kāi)始處空格的字符串

示例:

mysql> select length(concat('-',' mysql ','-')),length(concat('-',ltrim(' mysql '),'-'));
+-----------------------------------+------------------------------------------+
  length(concat('-',' mysql ','-'))   length(concat('-',ltrim(' mysql '),'-'))  
+-----------------------------------+------------------------------------------+
  9   8  
+-----------------------------------+------------------------------------------+
1 row in set (0.00 sec)

1.7.2 去除字符串結(jié)束處的空格:

rtrim(str) //返回去掉結(jié)束處空格的字符串。

示例:

mysql> select length(concat('-',' mysql ','-')) ,length(concat('-',rtrim(' mysql '),'-'));
+-----------------------------------+------------------------------------------+
  length(concat('-',' mysql ','-'))   length(concat('-',rtrim(' mysql '),'-'))  
+-----------------------------------+------------------------------------------+
  9   8  
+-----------------------------------+------------------------------------------+
1 row in set (0.00 sec)

1.7.3 去除字符串首尾空格:

trim(str) //返回去掉首尾空格的字符串

示例:

mysql> select concat(' mysql ') origi,length(concat(' mysql ')) orilen, concat(trim(' mysql ')) after, length(concat(trim(' mysql '))) afterlen;
+---------+--------+-------+----------+
  origi   orilen   after   afterlen  
+---------+--------+-------+----------+
  mysql   7   mysql   5  
+---------+--------+-------+----------+
1 row in set (0.00 sec)

1.8 替換字符串:

實(shí)現(xiàn)替換字符串的功能,分別為insert()和replace()

1.8.1 使用insert()函數(shù):

函數(shù)定義為:

insert(str,pos,len,newstr)
//insert()函數(shù)會(huì)將字符串str中的pos位置開(kāi)始長(zhǎng)度為len的字符串用字符串newstr來(lái)替換。
//如果參數(shù)pos的值超過(guò)字符串長(zhǎng)度,則返回值為原始字符串str。
//如果len的長(zhǎng)度大于原來(lái)str中所剩字符串的長(zhǎng)度,則從位置pos開(kāi)始進(jìn)行全部替換。若任何一個(gè)參數(shù)為null,則返回值為null.

示例:

mysql> select insert('這是mysql數(shù)據(jù)庫(kù)系統(tǒng)',3,5,'oracle') bieming;
+----------------------+
  bieming  
+----------------------+
  這oracleql數(shù)據(jù)庫(kù)系統(tǒng)  
+----------------------+
1 row in set, 1 warning (0.00 sec)

1.8.1 使用replace()函數(shù):

函數(shù)的定義為:

replace(str,substr,newstr) //將字符串str中的子字符串substr用字符串newstr來(lái)替換。

示例:

mysql> select replace('這是mysql數(shù)據(jù)庫(kù)','mysql','db2') bieming;
+---------------+
  bieming  
+---------------+
  這是db2數(shù)據(jù)庫(kù)  
+---------------+
1 row in set, 1 warning (0.00 sec)

2. 使用數(shù)值函數(shù):

2.1 獲取隨機(jī)數(shù):

通過(guò)rand()和rand(x)函數(shù)來(lái)獲取隨機(jī)數(shù)。這兩個(gè)函數(shù)都會(huì)返回0-1之間的隨機(jī)數(shù),其中rand()函數(shù)返回的數(shù)是完全隨機(jī)的,而rand(x)函數(shù)返回的隨機(jī)數(shù)值是完全相同的。

示例:

mysql> select rand(),rand(),rand(3),rand(3);
+--------------------+--------------------+--------------------+--------------------+
  rand()   rand()   rand(3)   rand(3)  
+--------------------+--------------------+--------------------+--------------------+
  0.9600886758045188   0.7006410161970565   0.9057697559760601   0.9057697559760601  
+--------------------+--------------------+--------------------+--------------------+
1 row in set (0.00 sec)

2.2 獲取整數(shù)的函數(shù):

在具體應(yīng)用中,如果想要獲取整數(shù),可以通過(guò)ceil()和floor()函數(shù)來(lái)實(shí)現(xiàn)。

ceil()函數(shù)的定義為:

ceil(x) //函數(shù)返回大于或等于數(shù)值x的最小整數(shù)。
floor() //函數(shù)返回小于或等于數(shù)值x的最大整數(shù)。

示例:

mysql> select ceil(4.3),ceil(-2.5),floor(4.3),floor(-2.5);
+-----------+------------+------------+-------------+
  ceil(4.3)   ceil(-2.5)   floor(4.3)   floor(-2.5)  
+-----------+------------+------------+-------------+
  5   -2   4   -3  
+-----------+------------+------------+-------------+
1 row in set (0.00 sec)

2.3 截取數(shù)值函數(shù):

可以通過(guò)truncate()對(duì)數(shù)值的小數(shù)位進(jìn)行截取:

函數(shù)定義為:

truncate(x,y) //返回?cái)?shù)值x,保留小數(shù)點(diǎn)后y位

示例:

mysql> select truncate(903.343434,2),truncate(903.343,-1);
+------------------------+----------------------+
  truncate(903.343434,2)   truncate(903.343,-1)  
+------------------------+----------------------+
  903.34   900  
+------------------------+----------------------+
1 row in set (0.00 sec)

2.4 四舍五入函數(shù):

對(duì)數(shù)值進(jìn)行四舍五入可以通過(guò)round()函數(shù)實(shí)現(xiàn):
round(x)
//函數(shù)返回值x經(jīng)過(guò)四舍五入操作后的數(shù)值。
round(x,y)
//返回?cái)?shù)值x保留到小數(shù)點(diǎn)后y位的值。在具體截取數(shù)據(jù)時(shí)需要進(jìn)行四舍五入的操作。

示例:

mysql> select round(903.53567),round(-903.53567),round(903.53567,2),round(903.53567,-1);
+------------------+-------------------+--------------------+---------------------+
  round(903.53567)   round(-903.53567)   round(903.53567,2)   round(903.53567,-1)  
+------------------+-------------------+--------------------+---------------------+
  904   -904   903.54   900  
+------------------+-------------------+--------------------+---------------------+
1 row in set (0.00 sec)

3. 使用日期和時(shí)間函數(shù):

3.1 獲取當(dāng)前日期和時(shí)間的函數(shù):

3.1.1 獲取當(dāng)前日期和時(shí)間(日期 + 時(shí)間):

MySQL中可以通過(guò)四個(gè)函數(shù)獲取當(dāng)前日期和時(shí)間,分別是now(),current_timestamp(),localtime(),sysdate(),這四個(gè)函數(shù)不僅可以獲取當(dāng)前日期和時(shí)間,而且顯示的格式也一樣。推薦使用now()

示例:

mysql> select now(),current_timestamp(),localtime(),sysdate();
+---------------------+---------------------+---------------------+---------------------+
  now()   current_timestamp()   localtime()   sysdate()  
+---------------------+---------------------+---------------------+---------------------+
  2016-08-25 16:09:20   2016-08-25 16:09:20   2016-08-25 16:09:20   2016-08-25 16:09:20  
+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)

3.1.2 獲取當(dāng)前日期:

獲取當(dāng)前日期的函數(shù)curdate()和current_date()函數(shù)。

示例:

mysql> select curdate(),current_date();
+------------+----------------+
  curdate()   current_date()  
+------------+----------------+
  2016-08-25   2016-08-25  
+------------+----------------+
1 row in set (0.00 sec)

3.1.3 獲取當(dāng)前時(shí)間:

獲取當(dāng)前時(shí)間的函數(shù),curtime()或者current_time();推薦使用curtime();

示例:

mysql> select curtime(),current_time();
+-----------+----------------+
  curtime()   current_time()  
+-----------+----------------+
  16:15:04   16:15:04  
+-----------+----------------+
1 row in set (0.00 sec)

3.2 獲取日期和時(shí)間各部分值:

在MySQL中,可以通過(guò)各種函數(shù)來(lái)獲取當(dāng)前日期和時(shí)間的各部分值,其中year()函數(shù)返回日期中的年份,quarter()函數(shù)返回日期屬于第幾個(gè)季度,month()函數(shù)返回日期屬于第幾個(gè)月,week()函數(shù)返回日期屬于第幾個(gè)星期,dayofmonth()函數(shù)返回日期屬于當(dāng)前月的第幾天,hour()函數(shù)返回時(shí)間的小時(shí),minute()函數(shù)返回時(shí)間的分鐘,second()函數(shù)返回時(shí)間的秒。

示例:

mysql> select now(),year(now()),quarter(now()),month(now()),week(now()),dayofmonth(now()),hour(now()),minute(now()),second(now());
+---------------------+-------------+----------------+--------------+-------------+-------------------+-------------+---------------+---------------+
  now()   year(now())   quarter(now())   month(now())   week(now())   dayofmonth(now())   hour(now())   minute(now())   second(now())  
+---------------------+-------------+----------------+--------------+-------------+-------------------+-------------+---------------+---------------+
  2016-08-25 16:27:37   2016   3   8   34   25   16   27   37  
+---------------------+-------------+----------------+--------------+-------------+-------------------+-------------+---------------+---------------+
1 row in set (0.00 sec)

3.2.1 關(guān)于月的函數(shù):

示例:

mysql> select now(),month(now()),monthname(now());
+---------------------+--------------+------------------+
  now()   month(now())   monthname(now())  
+---------------------+--------------+------------------+
  2016-08-25 16:29:37   8   August  
+---------------------+--------------+------------------+
1 row in set (0.00 sec)
//month()函數(shù)返回?cái)?shù)字表示的月份,monthname()函數(shù)返回了英文表示的月份。

3.2.2 關(guān)于星期的函數(shù):

示例:

mysql> select now(),week(now()),weekofyear(now()),dayname(now()),dayofweek(now()),weekday(now());
+---------------------+-------------+-------------------+----------------+------------------+----------------+
  now()   week(now())   weekofyear(now())   dayname(now())   dayofweek(now())   weekday(now())  
+---------------------+-------------+-------------------+----------------+------------------+----------------+
  2016-08-25 16:34:35   34   34   Thursday   5   3  
+---------------------+-------------+-------------------+----------------+------------------+----------------+
1 row in set (0.00 sec)

3.2.3 關(guān)于天的函數(shù):

示例:

mysql> select now(),dayofyear(now()),dayofmonth(now());
+---------------------+------------------+-------------------+
  now()   dayofyear(now())   dayofmonth(now())  
+---------------------+------------------+-------------------+
  2016-08-25 16:37:12   238   25  
+---------------------+------------------+-------------------+
1 row in set (0.00 sec)

3.2.4 獲取指定值的extract():

函數(shù)定義為:

extract(type from date)
//上述函數(shù)會(huì)從日期和時(shí)間參數(shù)date中獲取指定類型參數(shù)type的值。type的取值可以是:year,month,day,hour,minute和second

示例:

mysql> select now(),extract(year from now()) year,extract(month from now()) month,extract(day from now()) day,extract(hour from now()) hour,extract(mi
nute from now()) minute,extract(second from now()) second;
+---------------------+------+-------+------+------+--------+--------+
  now()   year   month   day   hour   minute   second  
+---------------------+------+-------+------+------+--------+--------+
  2016-08-25 16:43:45   2016   8   25   16   43   45  
+---------------------+------+-------+------+------+--------+--------+
1 row in set (0.00 sec)

3.3 計(jì)算日期和時(shí)間的函數(shù):

3.3.1 與默認(rèn)日期和時(shí)間操作:

兩個(gè)函數(shù)來(lái)實(shí)現(xiàn)與默認(rèn)日期和時(shí)間的操作,分別為to_days()和from_days()

to_days(date):該函數(shù)計(jì)算日期參數(shù)date與默認(rèn)日期和時(shí)間(0000年1月1日)之間的想個(gè)天數(shù)。

from_days(number):該函數(shù)計(jì)算從默認(rèn)日期和時(shí)間(0000年1月1日)開(kāi)始經(jīng)歷number天后的日期和時(shí)間。

示例:

mysql> select now(),to_days(now()),from_days(to_days(now()));
+---------------------+----------------+---------------------------+
  now()   to_days(now())   from_days(to_days(now()))  
+---------------------+----------------+---------------------------+
  2016-08-25 16:50:30   736566   2016-08-25  
+---------------------+----------------+---------------------------+
1 row in set (0.00 sec)
//指定兩個(gè)日期之間相隔的天數(shù);
mysql> select now(),datediff(now(),'2000-12-01');
+---------------------+------------------------------+
  now()   datediff(now(),'2000-12-01')  
+---------------------+------------------------------+
  2016-08-25 16:52:16   5746  
+---------------------+------------------------------+
1 row in set (0.00 sec)

3.3.2 與指定日期和時(shí)間操作:

adddate(date,n)函數(shù):該函數(shù)計(jì)算日期參數(shù)date加上n天后的日期。

subdate(date,n)函數(shù):該函數(shù)計(jì)算日期參數(shù)date減去n天后的日期。

adddate(d,interval expr type):返回日期參數(shù)d加上一段時(shí)間后的日期,表達(dá)式參數(shù)expr決定了時(shí)間的長(zhǎng)度,參數(shù)type決定了所操作的對(duì)象。

subdate(d,interval expr type):返回日期參數(shù)d減去一段時(shí)間后的日期,表達(dá)式expr決定了時(shí)間的長(zhǎng)度。參數(shù)type決定了所操作的對(duì)象。

addtime(time,n):計(jì)算時(shí)間參數(shù)time加上n秒后的時(shí)間。

subtime(time,n):計(jì)算時(shí)間參數(shù)time減去n秒后的時(shí)間。

示例一:

mysql> select curdate(),adddate(curdate(),5),subdate(curdate(),5);
+------------+----------------------+----------------------+
  curdate()   adddate(curdate(),5)   subdate(curdate(),5)  
+------------+----------------------+----------------------+
  2016-08-25   2016-08-30   2016-08-20  
+------------+----------------------+----------------------+
1 row in set (0.00 sec)

示例二:

mysql> select curdate(),adddate(curdate(),interval '2,3' year_month),subdate(curdate(),interval '2,3' year_month);
+------------+----------------------------------------------+----------------------------------------------+
  curdate()   adddate(curdate(),interval '2,3' year_month)   subdate(curdate(),interval '2,3' year_month)  
+------------+----------------------------------------------+----------------------------------------------+
  2016-08-25   2018-11-25   2014-05-25  
+------------+----------------------------------------------+----------------------------------------------+
1 row in set (0.00 sec)

示例三:

mysql> select curtime(),addtime(curtime(),5),subtime(curtime(),5);
+-----------+----------------------+----------------------+
  curtime()   addtime(curtime(),5)   subtime(curtime(),5)  
+-----------+----------------------+----------------------+
  17:12:21   17:12:26   17:12:16  
+-----------+----------------------+----------------------+
1 row in set (0.00 sec)

4. 使用系統(tǒng)信息函數(shù):

select version(),database(),user();

示例:

mysql> select version(),database(),user();
+------------+------------+----------------+
  version()   database()   user()  
+------------+------------+----------------+
  5.5.51-log   NULL   root@localhost  
+------------+------------+----------------+
1 row in set (0.00 sec)
//獲取 auto_increment約束的最后ID
select last_insert_id();

相關(guān)推薦:

php正則表達(dá)式中常用函數(shù)的詳解

詳解Oracle常用函數(shù)Trunc

php中關(guān)于常用函數(shù)整理總結(jié)

以上就是MySQL中的常用函數(shù)詳解的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!


學(xué)習(xí)教程快速掌握從入門(mén)到精通的SQL知識(shí)。