LEFT(str,length)
作用:取出串str左邊長度為length的子串;
RIGHT(str,length)
作用:取出串str右邊邊長度為length的子串都许;
SUBSTRING(str,startIndex)
作用:取出str,從第startIndex開始到末尾位置長度的串稻薇;
SUBSTRING(str,startIndex,length)
作用:取出str,從第startIndex開始長度為length的串胶征;
UPPER(str)
作用:將字符串str轉(zhuǎn)為大寫塞椎;
LOWER()
作用:將字符串str轉(zhuǎn)為小寫形式;
LENGTH()
作用:顯示字符串str的長度睛低;
LOCATE(substr,str)
作用:找出substr子串在str中第一次出現(xiàn)的位置案狠;
mysql> SELECT LEFT('www.baidu.com',4);
+-------------------------+
| LEFT('www.baidu.com',4) |
+-------------------------+
| www. |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT RIGHT('www.baidu.com',4);
+--------------------------+
| RIGHT('www.baidu.com',4) |
+--------------------------+
| .com |
+--------------------------+
1 row in set (0.00 sec)
mysql> SELECT SUBSTRING('www.baidu.com',4);
+------------------------------+
| SUBSTRING('www.baidu.com',4) |
+------------------------------+
| .baidu.com |
+------------------------------+
1 row in set (0.00 sec)
mysql> SELECT SUBSTRING('www.baidu.com',4,3);
+--------------------------------+
| SUBSTRING('www.baidu.com',4,3) |
+--------------------------------+
| .ba |
+--------------------------------+
1 row in set (0.00 sec)
+------------------------+
| UPPER('www.baidu.com') |
+------------------------+
| WWW.BAIDU.COM |
+------------------------+
1 row in set (0.00 sec)
mysql> SELECT LOWER('WWW.BAIDU.COM');
+------------------------+
| LOWER('WWW.BAIDU.COM') |
+------------------------+
| www.baidu.com |
+------------------------+
1 row in set (0.00 sec)
mysql> SELECT LENGTH('WWW.BAIDU.COM');
+-------------------------+
| LENGTH('WWW.BAIDU.COM') |
+-------------------------+
| 13 |
+-------------------------+
1 row in set (0.00 sec)
mysql> SELECT LOCATE('bai','www.baidu.com');
+-------------------------------+
| LOCATE('bai','www.baidu.com') |
+-------------------------------+
| 5 |
+-------------------------------+
1 row in set (0.00 sec)
關(guān)于日期
函數(shù) | 說明 |
---|---|
ADDDATE() | 增加一個日期(天、周)钱雷,也可以使用INTERVAL類似于DATE_ADD() |
ADDTIME() | 增加一個時間(時骂铁、分等) |
CURDATE() | 返回當(dāng)前日期 |
CURTIME() | 返回當(dāng)前日期 |
DATE() | 返回日期時間的日期部分 |
DATEDIFF() | 計算兩個日期之間的差 |
DATE_ADD() | 非常靈活的日期函數(shù) |
DATE_FORMAT() | 日期格式化 |
DAY() | 返回日期的天數(shù)部分 |
DAYOFFWEEK() | 返回星期幾,1表示周日罩抗; |
HOUR() | 返回日期時間的小時部分 |
MINUTE() | 返回日期時間的分部分拉庵; |
MONTH() | 返回日期時間的月份 |
NOW() | 返回當(dāng)前日期時間 |
SECOND() | 返回日期時間的秒 |
TIME() | 返回日期時間的時間部分 |
YEAR() | 返回一個日期的年份部分; |
mysql> SELECT CURDATE();# 返回當(dāng)前的日期套蒂,不包括時間钞支;
+------------+
| CURDATE() |
+------------+
| 2020-05-04 |
+------------+
1 row in set (0.00 sec)
mysql> SELECT CURTIME();#返回當(dāng)前的時間茫蛹;
+-----------+
| CURTIME() |
+-----------+
| 18:52:01 |
+-----------+
1 row in set (0.00 sec)
mysql> SELECT DATE(NOW());#返回日期部分;
+-------------+
| DATE(NOW()) |
+-------------+
| 2020-05-04 |
+-------------+
1 row in set (0.00 sec)
mysql> SELECT DAY(NOW());#返回日期的天數(shù)部分烁挟;
+------------+
| DAY(NOW()) |
+------------+
| 4 |
+------------+
1 row in set (0.00 sec)
mysql> SELECT HOUR(NOW());#返回時間的小時部分婴洼;
+-------------+
| HOUR(NOW()) |
+-------------+
| 18 |
+-------------+
1 row in set (0.00 sec)
mysql> SELECT MINUTE(NOW());#返回時間的分部分;
+---------------+
| MINUTE(NOW()) |
+---------------+
| 56 |
+---------------+
1 row in set (0.00 sec)
mysql> SELECT SECOND(NOW());#返回時間的秒部分信夫;
+---------------+
| SECOND(NOW()) |
+---------------+
| 22 |
+---------------+
1 row in set (0.00 sec)
mysql> SELECT MONTH(NOW());#返回日期的月部分窃蹋;
+--------------+
| MONTH(NOW()) |
+--------------+
| 5 |
+--------------+
1 row in set (0.00 sec)
mysql> SELECT TIME(NOW());#返回時間部分;
+-------------+
| TIME(NOW()) |
+-------------+
| 18:56:48 |
+-------------+
1 row in set (0.00 sec)
mysql> SELECT YEAR(NOW());#返回日期的年份静稻;
+-------------+
| YEAR(NOW()) |
+-------------+
| 2020 |
+-------------+
1 row in set (0.00 sec)
mysql> SELECT DAYOFWEEK(NOW());#返回星期幾警没,這里周日是1;
+------------------+
| DAYOFWEEK(NOW()) |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_FORMAT(NOW(),'%Y/%m/%d');#格式化日期時間振湾;
+-------------------------------+
| DATE_FORMAT(NOW(),'%Y/%m/%d') |
+-------------------------------+
| 2020/05/04 |
+-------------------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_ADD(NOW(),INTERVAL 2 DAY);#更改日期時間杀迹,比較靈活,INTERVAL押搪。
+--------------------------------+
| DATE_ADD(NOW(),INTERVAL 2 DAY) |
+--------------------------------+
| 2020-05-06 19:06:03 |
+--------------------------------+
1 row in set (0.00 sec)
mysql> SELECT ADDDATE(NOW(),2);#增加一個日期树酪;
+---------------------+
| ADDDATE(NOW(),2) |
+---------------------+
| 2020-05-06 19:08:09 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT ADDDATE(NOW(),INTERVAL 2 DAY);#可以和DATE_ADD一樣使用;
+-------------------------------+
| ADDDATE(NOW(),INTERVAL 2 DAY) |
+-------------------------------+
| 2020-05-06 19:08:20 |
+-------------------------------+
1 row in set (0.00 sec)
mysql> SELECT DATEDIFF(NOW(),'2020-09-01');#日期相減大州;
+------------------------------+
| DATEDIFF(NOW(),'2020-09-01') |
+------------------------------+
| -120 |
+------------------------------+
1 row in set (0.00 sec)
mysql> SELECT ADDTIME(NOW(),'1:00:00');#新增時間续语,時分秒等;
+--------------------------+
| ADDTIME(NOW(),'1:00:00') |
+--------------------------+
| 2020-05-04 20:13:59 |
+--------------------------+
1 row in set (0.00 sec)
數(shù)值處理函數(shù)
函數(shù) | 說明 |
---|---|
ABS() | 返回絕對值 |
COS() | 余弦值厦画; |
SIN() | 正弦值疮茄; |
TAN() | 正切值 |
EXP() | 指數(shù)值 |
MOD() | 兩個值的余數(shù)(*) |
PI() | 圓周率(*) |
RAND() | 返回一個隨機(jī)數(shù);(*) |
SQRT() | 平方根 |
mysql> SELECT ABS(-1);
+---------+
| ABS(-1) |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
mysql> SELECT COS(90);
+---------------------+
| COS(90) |
+---------------------+
| -0.4480736161291701 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT EXP(10);
+--------------------+
| EXP(10) |
+--------------------+
| 22026.465794806718 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT MOD(11,2);
+-----------+
| MOD(11,2) |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
mysql> SELECT PI();
+----------+
| PI() |
+----------+
| 3.141593 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT RAND();
+--------------------+
| RAND() |
+--------------------+
| 0.7575842177100333 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT SIN(90)
-> ;
+--------------------+
| SIN(90) |
+--------------------+
| 0.8939966636005579 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT SQRT(4);
+---------+
| SQRT(4) |
+---------+
| 2 |
+---------+
1 row in set (0.00 sec)
mysql> SELECT TAN(45)
-> ;
+--------------------+
| TAN(45) |
+--------------------+
| 1.6197751905438615 |
+--------------------+
1 row in set (0.00 sec)