mysql common mathematical functions

1. Mathematical Functions

function For work
ABS(x) Returns the absolute value of x
CEIL(x),CEILIN(x) Returns the smallest integer not less than x
FLOOR(x) Returns the maximum integer not greater than x
RAND() Returns a random number of 0 to 1,
RAND(x) Returns a random number of 0 to 1, returns the same value of x when the same random number Returns
SIGN(x) -1,0,1 return parameter as a symbol, depending on the symbol x is negative, zero or a positive
PI() Returns the PI pi, display the default number of decimal places is 7, the internal mysql have completely double-precision values
TRUNCATE(x,y) y-bit value returned retention decimal value x
ROUND(x) Return to nearest integer from x
ROUND(x,y) Retention x y bits after the decimal point, but cut to rounding
POW (x, y), POWER (x, y) The results of x y Power Back
SQRT(x) Returns the non-negative quadratic root of x
EXP(x) Returns the value e (natural logarithm of the bottom) of the power of x
V (x, y) Returns the remainder of x divided by y later
LOG(x) Back logarithm base x 2
LOG10(x) Returns the number of the base 10 of x
RADIANS(x) Converts degrees to radians
DEGREES(x) Radians converted to an angle
SIN (x) Returns the sine of x, where x is given in radians
ASIN(x) Returns the arc sine of x, that is the sine value of x. -1 to 1 if x is not on the return null
COS(x) Returns the cosine of x, where x is given in radians
ACOS(x) Returns the inverse cosine of x, i.e. the value of the cosine of x. -1 to 1 if x is not on the return null
TAN (x) Returns the arctangent, i.e. tangent value of x
ATAN (x), ATAN (x, y) Returns the two variables x and y arctangent.
COT(x) Returns the cotangent of x

 

2. String Functions

function For work
CHAR_LENGTH(s) The number of characters in the string s (length for the query string)
LENGTH(s) Returns the number of bytes in the string
CONCAT(s1,s2..) The stitching together several strings, if a return of null null
CONCAT_WS(x,s1,s2..) With concat (s1, s2 ..) function, but each string to be spliced ​​together with x
INSERT(s1,x,len,s2) Alternatively starting position x s1 is the length of the string s2 len is a string
UPPER(s),UCASE(s) 将字符串s的所有字母都变成大写
LOWER(s),LCASE(s) 将字符串s的所有字母都变成小写
LEFT(s,n) 返回字符串左边的n个字符串
RIGHT(s,n) 返回字符串右边的n个字符串
LTRIM(s) 返回去除了左边空格的s
RTRIM(s) 返回去除了右边空格的s
TRIM(s) 返回去除两边的空格的s
TRIM(s1 FROM s) 返回去除了两边s1的s
REPEAT(s,n) 将字符串s重复n次
SPACE(n) 返回n个空格
replace(s,s1,s2) 用s2代替s中的s1
STRCMP(s1,s2) 比较字符串s1和s2长度,s1长于s2则返回1,s2长于s1则返回-1,相等则返回0
SUBSTRING(s,n,len) 获取从字符串s的第n个位置开始长度为len的字符串
MID(s,n,len) 同SUBSTRING(s,n,len)
LOCATE(s1,s),POSITION(s1 IN s) 找到s中s1的位置
INSTER(s,s1) 查找字符串s1在s中的位置,返回首次出现位置的索引值
REVERSE(s) 将字符串s的顺序反过来
ELT(n,s1,s2) 返回第n个字符串
FIELD(s,s1,s2..) 返回第一个与字符串s匹配的字符串的位置
FIND_IN_SET(s1,s2) 返回在字符串s2中与s1匹配的字符串的位置

 

3. 日期和时间函数

函 数 作 用
CURDATE(),CURRENT_DATE() 返回当前时间
CURTIME(),CURRENT_TIME() 返回当前时间
NOW(),CURRENT_TIMESTAMP(),LOCALTIME(),SYSDATE(),LOCALTIMESTAMP() 返回当前日期和时间
UNIX_TIMESTAMP() 以unix时间戳形式返回当前时间
UNIX_TIMESTAMP(d) 将时间d以unix时间戳形式返回
FROM_UNIXTIME(d) 将unix时间戳的时间转换为普通格式的时间
UTC_DATE() 返回UTC日期
UTC_TIME() 返回UTC时间
MONTH(d) 返回日期d中的月份值,范围0~12
MONTHNAME(d) 返回日期d中的月份名称
DAYNAME(d) 返回日期d是星期几
DAYOFWEEK(d) 返回日期d是星期几,1是星期天,2是星期一
WEEKDAY(d) 返回日期d是星期几,0是星期一,1是星期二
WEEK(d) 计算日期d是本年的第几个星期,范围0~53
WEEKOFYEAR(d) 计算日期d是本年的第几个星期,范围1~53
DAYOFYEAR(d) 计算日期d是本年的第几天
DAYOFMONTH(d) 计算日期d是本月的第几天
YEAR(d) 返回年份值
QUARTER(d) 返回日期d是第几个季度,范围1~4
HOUR(t) 返回时间t中的小时值
MINUTE(t) 返回时间t中的分钟值
SECOND(t) 返回时间t中的秒钟值
EXTRACT(type of d) 从日期d中获取指定的值,type可以是YEAR,HOUR等
TIME_TO_SEC(t) 将时间t转换为秒
SEC_TO_TIME(s) 秒值s转换为时间格式
TO_DAYS(d) 计算d~0000年1月1日的天数
FROM_DAYS(n) 计算从0000年1月1日开始n天后的日期
DATEDIFF(d1,d2) 计算日期d1~d2之间相隔的天数
ADDDATE(d,n) 计算起始日期d加上n天的日期
SUBDATE(d,n) 计算起始日期d减去n天的日期
ADDTIME(t,n) 计算起始时间t加上n秒的时间
SUBTIME(t,n) 计算起始时间t减去n秒的时间
DATE_FORMAT(d,f) 按照表达式f的要求显示日期d
TIME_FORMAT(t,f) 按照表达式f的要求显示时间t
GET_FORMAT(type,s) 根据字符串s中获取type类型数据的显示格式

Guess you like

Origin www.cnblogs.com/ljt1412451704/p/12176155.html