This article explains the function method Yes Yes MySQL, MySQL cover all common methods. Here is the directory structure of this article, you can click on to find the right directory according to their needs:
- A digital function
- Second, String Functions
- Third, the date function
- 4, MySQL of advanced functions
(A) digital function
1, ABS (x) returns the absolute value of x
SELECT ABS (-1); ---- Returns 1
2, AVG (expression) Returns the average value of an expression, expression is a field
SELECT AVG(age) FROM student;
3, CEIL (x) / CEILING (x) is greater than or equal to x returns the smallest integer
SELECT CEIL(1.5);
SELECT CEILING(1.5);
---- returns 2
4, FLOOR (x) returns the largest integer less than or equal to x
SELECT FLOOR(1.5);
---- returns 1
5, EXP (x) returns the e-th power of x
SELECT EXP(3);
---- e calculated cubic returns 20.085536923188
6, GREATEST (expr1, expr2, expr3, ...) returns the maximum list
SELECT GREATEST(3, 12, 34, 8, 25);
---- returns the following list of maximum 34 figures
7, LEAST (expr1, expr2, expr3, ...) returns the minimum value in the list
SELECT LEAST(3, 12, 34, 8, 25);
---- return the following list of numbers of minimum 3
8, LN returns the natural logarithm of numbers
SELECT LN(2);
---- Returns the natural logarithm of 2: 0.6931471805599453
9, LOG (x) returns the natural logarithm (logarithm of base e)
SELECT LOG(20.085536923188);
---- returns 3
10, MAX (expression) returns the largest value of the field expression
SELECT MAX(age) AS maxAge FROM Student;
---- age maximum
11, MIN (expression) returns the largest value of the field expression
SELECT MIN(age) AS minAge FROM Student;
---- age minimum
12, POW (x, y) / POWER (x, y) returns x to the power y
SELECT POW(2,3);
SELECT POWER(2,3);
---- returns 2 3 power: 8
13, RAND () returns a random number between 0 and 1
SELECT RAND();
---- returns a random number from 0 to 1, if () there are digital, RAND (x), x is the same, the same as the return value
14, ROUND (x) returns the nearest integer from x
SELECT ROUND(1.23456);
---- returns 1
15, SIGN (x) returns the sign of x, x is negative, 0, -1, 0, and a positive number, respectively,
SELECT SIGN(-10);
---- -1
16, SQRT (x) returns the square root of x
SELECT SQRT(25);
5 ---- return
17, SUM (expression) Returns the specified field sum
SELECT SUM(age) AS totalAage FROM Student;
---- returns the sum of age
18, TRUNCATE (x, y) returns a value x value y bits after the decimal point retention (ROUND the biggest difference is not rounded)
SELECT TRUNCATE(1.23456,3);
---- returns 1.234
(B) String Functions
1, returns the string s first character ASCII code
SELECT ASCII('AB');
A return to the ASCII value ----: 65
2, LENGTH / CHAR_LENGTH (s) / CHARACTER_LENGTH (s) the number of characters in the string s
SELECT LENGTH('1234');
---- returns 4
3, CONCAT (s1, s2 ... sn) String s1, s2, more strings into a string
SELECT CONCAT ( 'hel', 'llo');
---- returns hello
4, FIND_IN_SET (s1, s2) returns the position in the string s2 s1 with the matching string
SELECT FIND_IN_SET("c", "a,b,c,d,e");
---- returns 3
5, FORMAT (x, n) function may be formatted digital x "#, ###. ##", after the decimal point of x n bits, the last bit rounded
SELECT FORMAT(250500.5634, 2);
---- return 250,500.56
6, INSERT (s1, x, len, s2) position start string s2 x s1 is replaced length len of the string
SELECT INSERT("google.com", 1, 6, "runnob");
---- return runoob.com
7, LOCATE (s1, s) from s1 acquisition start position of the string s
SELECT LOCATE('st','myteststring');
5 ---- return
8, LCASE (s) / LOWER (s) of the string s into all letters lower case letters
SELECT LOWER('RUNOOB');
---- return runoob
9, UCASE (s) / UPPER (s) all of the letter string s into an uppercase
SELECT UCASE('runoob');
---- return RUNOOB
10, TRIM (s) and remove the spaces at the beginning and end string s
SELECT TRIM(' RUNOOB ');
---- return RUNOOB
11, LTRIM (s) to remove the spaces at the beginning string s
SELECT LTRIM(' RUNOOB ');
---- return 'RUNOOB'
12, RTRIM (s) to remove the spaces at the end of the string s
SELECT RTRIM(' RUNOOB ');
---- return 'RUNOOB'
13, SUBSTR (s, start, length) taken from the start position is the length of the string s substring of length
SELECT SUBSTR("RUNOOB", 2, 3) AS ExtractString;
---- interception of three characters from a string RUNOOB in a second position, return UNO
14, SUBSTR / SUBSTRING (s, start, length) taken from the start position of the string s length substring of length
SELECT SUBSTR/SUBSTRING("RUNOOB", 2, 3);
---- interception of three characters from a string RUNOOB in a second position, return UN
15, POSITION (s1 IN s) from the capture start position of the string s s1
SELECT POSITION('b' in 'abc');
---- returns 2
16, REPEAT (s, n) is repeated n times the string s
SELECT REPEAT('runoob',3);
---- return runoobrunoobrunoob
17, REVERSE (s) will turn the order of the string s
SELECT REVERSE('abc');
---- return cba
18, STRCMP (s1, s2) compare strings s1 and s2, s1 and s2 are equal if returns 0 if s1> s2 returns 1 if s1 <s2 -1
SELECT STRCMP("runoob", "runoob");
Return 0 ----
(C) Date Functions
1, CURDATE () / CURRENT_DATE () Returns the current date
SELECT CURDATE();
SELECT CURRENT_DATE();
---- return 2019-02-19
2, CURRENT_TIME () / CURTIME () Returns the current time
SELECT CURRENT_TIME();
---- return 11:40:45
3, CURRENT_TIMESTAMP () returns the current date and time
SELECT CURRENT_TIMESTAMP();
---- return 2019-02-19 11:41:32
4, ADDDATE (d, n) is calculated starting date Date d plus n days
SELECT ADDDATE("2017-06-15", INTERVAL 10 DAY);
---- return 2017-06-25
5, ADDTIME (t, n) at time t plus n seconds
SELECT ADDTIME('2011-11-11 11:11:11', 5);
---- return 2011-11-11 11:11:16
6, DATE () extracted from the date or time expression date value
SELECT DATE("2017-06-15 11:11:16");
---- return 2017-06-15
7, DAY (d) Returns the date portion of the date value d
SELECT DAY("2017-06-15");
---- return 15
8, DATEDIFF (d1, d2) calculating date d1-> the number of days between d2
SELECT DATEDIFF('2001-01-01','2001-02-02');
---- returns -32
9, DATE_FORMAT d required to display the date of the expression f
SELECT DATE_FORMAT('2011.11.11 11:11:11','%Y-%m-%d %r');
---- return 2011-11-11 11:11:11 AM
10, DAYNAME (d) d returns the date of the week, such as Monday, Tuesday
SELECT DAYNAME('2011-11-11 11:11:11');
---- return Friday
11, DAYOFMONTH (d) d calculation date is the first few days of the month
SELECT DAYOFMONTH('2011-11-11 11:11:11');
---- return 11
12, DAYOFWEEK (d) Date d day is today, 1 Sunday, 2 Monday, and so on
SELECT DAYOFWEEK('2011-11-11 11:11:11');
---- 6 return
13, DAYOFYEAR (d) d is calculated day of the date of the year
SELECT DAYOFYEAR('2011-11-11 11:11:11');
---- return 315
14, EXTRACT (type FROM d) acquired from the date specified value d, the return value of the type designated
type可取值为:
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_SECOND
DAY_MINUTE
DAY_HOUR
YEAR_MONTH
SELECT EXTRACT(MINUTE FROM '2011-12-13 14:15:16');
---- return 15
15, DAYOFWEEK (d) Date d day is today, 1 Sunday, 2 Monday, and so on
SELECT DAYOFWEEK('2011-11-11 11:11:11');
---- 6 return
16, UNIX_TIMESTAMP () stamp obtained
SELECT UNIX_TIMESTAMP('2019-2-19');
SELECT UNIX_TIMESTAMP(expression);
---- return 1550505600
17, FROM_UNIXTIME () transfer date stamp
SELECT FROM_UNIXTIME(1550505600);
---- 返回2019-02-19 00:00:00 SELECT FROM_UNIXTIME(1550505600, '%Y-%m-%d');
---- return 2019-02-19
(D) MySQL advanced functions
1, IF (expr, v1, v2) established if the expression expr, returns the result v1; otherwise, it returns the results v2
SELECT IF(1>0,'yes','no');
---- returns yes
2, CONV (x, f1, f2) returns into decimal number f1 f2 nary
SELECT CONV(13,10,2);
---- 1101 return
3, CURRENT_USER () / SESSION_USER ( ) / SYSTEM_USER () / USER () Returns the current user
4, DATABASE () Returns the current database name
5, VERSION () returns the version number of the database