Mysql数据库函数(数字,字符串,日期时间)
文章目錄
- Mysql數據庫函數(數字,字符串,日期時間)
- 數學函數
- 字符串函數
- 日期函數
Mysql數據庫函數(數字,字符串,日期時間)
數學函數
- abs(x) :返回x的絕對值
- rand() :返回0到1的隨機數 注意:取不到1
- mod(x,y):返回x除以y以后的余數
- power(x,y):返回x的y次方
- round(x):返回離x最近的整數
- sqrt(x):返回x的平方根
- truncate(x,y):返回數字x截斷為y位小數的值
- ceil(x):返回大于或等于x的最小整數 ## 注意:是向上取整數
- floor(x):返回小于或等于x的最大整數 ## 注意:是向下取整數
- greatest(x1,x2…):返回集合中最大的值
- least(x1,x2…):返回集合中最小的值
常用的數學函數
abs(x)返回x的絕對值
rand()返回0到1的隨機數
mysql> select rand(); +--------------------+ | rand() | +--------------------+ | 0.6230614116630158 | +--------------------+ 1 row in set (0.00 sec)mod(x,y)返回x除以y以后的余數
mysql> select mod(10,3); +-----------+ | mod(10,3) | +-----------+ | 1 | +-----------+ 1 row in set (0.00 sec)power(x,y)返回x的y次方
mysql> select power(3,2); +------------+ | power(3,2) | +------------+ | 9 | +------------+ 1 row in set (0.00 sec)round(x)返回離x最近的整數
mysql> select round(1.49); +-------------+ | round(1.49) | +-------------+ | 1 | +-------------+round(x)返回離x最近的整數
mysql> select round(1.49); +-------------+ | round(1.49) | +-------------+ | 1 | +-------------+round(x,y)保留x的y位小數四舍五入后的值
mysql> select round(1.51); +-------------+ | round(1.51) | +-------------+ | 2 | +-------------+ 1 row in set (0.00 sec)mysql> select round(1.51,1); 保留一位有效數字
+---------------+ | round(1.51,1) | +---------------+ | 1.5 | +---------------+ 1 row in set (0.00 sec)sqrt(x)返回x的平方根
mysql> select sqrt(4); +---------+ | sqrt(4) | +---------+ | 2 | +---------+ 1 row in set (0.00 sec)truncate(x,y)返回數字x截斷為y位小數的值
mysql> select truncate(3.1415926,3); +-----------------------+ | truncate(3.1415926,3) | +-----------------------+ | 3.141 | +-----------------------+ 1 row in set (0.00 sec)ceil(x)返回大于或等于x的最小整數
mysql> select ceil(1.9); +-----------+ | ceil(1.9) | +-----------+ | 2 | +-----------+ 1 row in set (0.00 secfloor(x)返回小于或等于x的最大整數
mysql> select floor(2.9); +------------+ | floor(2.9) | +------------+ | 2 | +------------+ 1 row in set (0.00 sec)greatest(x1,×2.-.)返回集合中最大的值
least(x1,x2…)返回集合中最小的值
字符串函數
- length(x):返回字符串x的長度
- trim():返回去除指定格式的值
- concat(x,y):將提供的參數x和y拼接成一個字符串
- upper(x):將字符串x的所有字母變成大寫字母
- lower(x):將字符串x的所有字母變成小寫字母
- left(x,y):返回字符串x的前y個字符
- right(x,y):返回字符串x的后y個字符
- repeat(x,y):將字符串x重復y次
- space(x):返回x個空格
- replace(x,y,z):將字符串z替代字符串x中的字符串y
- strcmp(x,y):比較x和y,返回的值可以為-1,0,1 ## 注意:只能進行個位運算
- substring(z,y,z):獲取從字符串x中的第y個位置開始長度為z的字符串
- reverse(x):將字符串x反轉
- length(x)返回字符串x的長度
trim()返回去除指定格式的值
mysql> select trim(' bdqn '); +----------------+ | trim(' bdqn ') | +----------------+ | bdqn | +----------------+concat(x.y)將提供的參數x和y拼接成一個字符串
mysql> select concat('abc','def'); +---------------------+ | concat('abc','def') | +---------------------+ | abcdef | +---------------------+ 1 row in set (0.00 sec)綜合應用
mysql> select concat ('abc',trim('bdqn')); +-----------------------------+ | concat ('abc',trim('bdqn')) | +-----------------------------+ | abcbdqn | +-----------------------------+ 1 row in set (0.00 sec)upper(x)將字符串x的所有字母變成大寫字母
lower(x)將字符串x的所有字母變成小寫字母
left(x,y)返回字符串x的前y個字符
mysql> select left('adcdefg',4); +-------------------+ | left('adcdefg',4) | +-------------------+ | adcd | +-------------------+ 1 row in set (0.00 sec)right(x,y)返回字符串x的后y個字符
mysql> select right('abcdefg',3); +--------------------+ | right('abcdefg',3) | +--------------------+ | efg | +--------------------+ 1 row in set (0.00 sec)綜合實驗
mysql> select concat(left('adcdef',3),right('abcdefg',3)); +---------------------------------------------+ | concat(left('adcdef',3),right('abcdefg',3)) | +---------------------------------------------+ | adcefg | +---------------------------------------------+ 1 row in set (0.00 sec)repeat(x,y)將字符串x重復y次
mysql> select repeat('abc',2); +-----------------+ | repeat('abc',2) | +-----------------+ | abcabc | +-----------------+ 1 row in set (0.00 sec)space(x))返回x個空格
mysql> select concat('a',space('4'),'b'); +----------------------------+ | concat('a',space('4'),'b') | +----------------------------+ | a b | +----------------------------+ 1 row in set (0.00 sec)replace(x.y,z)將字符串z替代字符串x中的字符串y
mysql> select replace('abc','bc','de'); +--------------------------+ | replace('abc','bc','de') | +--------------------------+ | ade | +--------------------------+ 1 row in set (0.00 sec)secstrcmp(x,y)比較x和y,返回的值可以為-1小于,0等于 ,1大于
mysql> select strcmp(9,7); +-------------+ | strcmp(9,7) | +-------------+ | 1 | +-------------+ 1 row in set (0.00 sec)mysql> select strcmp(5,6); +-------------+ | strcmp(5,6) | +-------------+ | -1 | +-------------+ 1 row in set (0.00 sec)mysql> select strcmp(5,5); +-------------+ | strcmp(5,5) | +-------------+ | 0 | +-------------+ 1 row in set (0.00 sec)substring(x,y,z)獲取從字符串x中的第y個位置開始長度為z的字符串
mysql> select substring('adcdefg',3,4); +--------------------------+ | substring('adcdefg',3,4) | +--------------------------+ | cdef | +--------------------------+ 1 row in set (0.00 sec)reverse(x)將字符串x反轉
mysql> select reverse ('hello'); +-------------------+ | reverse ('hello') | +-------------------+ | olleh | +-------------------+ 1 row in set (0.00 sec)日期函數
- curdate(x):返回當前時間的年月日
- curtime(x):返回當前時間的時分秒
- now(x):返回房錢時間的日期和時間
- month(x):返回日期x中的月份值
- week(x):返回日期x是年度第幾個星期
- hour(x):返回x中的小時值
- minute(x):返回x中的分鐘值
- second(x):返回x中的秒鐘值
- dayofweek(x):返回x是星期幾,1星期日,2星期一
- dayofmonth(x):計算日期x是本月的第幾天
- dayofyear(x):(x):計算日期x是本年的第幾天
curdate()返回當前時間的年月日
mysql> select curdate(); +------------+ | curdate() | +------------+ | 2020-08-25 | +------------+ 1 row in set (0.00 sec)curtime()返回當前時間的時分秒
mysql> select curtime(); +-----------+ | curtime() | +-----------+ | 16:30:34 | +-----------+ 1 row in set (0.00 sec)now()返回當前時間的日期和時間
mysql> select now(); +---------------------+ | now() | +---------------------+ | 2020-08-25 16:29:37 | +---------------------+ 1 row in set (0.00 sec)month(x)返回日期x中的月份值
mysql> select month('2020-08-25'); +---------------------+ | month('2020-08-25') | +---------------------+ | 8 | +---------------------+ 1 row in set (0.00 sec)week(x)返回日期x是年度第幾個星期
mysql> select week('2020-08-25'); +--------------------+ | week('2020-08-25') | +--------------------+ | 34 | +--------------------+ 1 row in set (0.00 sec)hour(x)返回x中的小時值
mysql> select hour(curtime()); +-----------------+ | hour(curtime()) | +-----------------+ | 16 | +-----------------+ 1 row in set (0.00 sec)minute(x)返回x中的分鐘值
mysql> select minute(curtime()); +-------------------+ | minute(curtime()) | +-------------------+ | 36 | +-------------------+ 1 row in set (0.00 sec)second(x)返回x中的秒鐘值
mysql> select second(curtime()); +-------------------+ | second(curtime()) | +-------------------+ | 58 | +-------------------+ 1 row in set (0.00 sec)dayofweek(x)返回x是星期幾,1星期日,2星期
mysql> select dayofweek(curtime()); +----------------------+ | dayofweek(curtime()) | +----------------------+ | 3 | +----------------------+ 1 row in set (0.00 sec)dayofmonth(x)計算日期x是本月的第幾天
mysql> select dayofmonth(curtime()); +-----------------------+ | dayofmonth(curtime()) | +-----------------------+ | 25 | +-----------------------+ 1 row in set (0.00 sec)dayofyear(x)計算日期x是本年的第幾天
mysql> select dayofyear(curtime()); +----------------------+ | dayofyear(curtime()) | +----------------------+ | 238 | +----------------------+ 1 row in set (0.00 sec)總結
以上是生活随笔為你收集整理的Mysql数据库函数(数字,字符串,日期时间)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内存条双通道测试:性能大揭秘,选对内存条
- 下一篇: mysql5.7主从同步与读写分离