Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL Functions

MySQL Functions

MySQL has many built-in functions. The following lists the descriptions of these functions.


MySQL String Functions

function Description Example
ASCII(s) Returns the ASCII code of the first character of the string s.

Returns the ASCII code of the first letter of the CustomerName field:

SELECT ASCII(CustomerName) AS NumCodeOfFirstChar
FROM Customers;
CHAR_LENGTH(s) Returns the number of characters in the string s

Returns the number of characters in the string RUNOOB

SELECT CHAR_LENGTH("RUNOOB") AS LengthOfString; | | CHARACTER_LENGTH(s) | Returns the number of characters in the string s, equivalent to CHAR_LENGTH(s) |

Returns the number of characters in the string RUNOOB

SELECT CHARACTER_LENGTH("RUNOOB") AS LengthOfString;

| | CONCAT(s1,s2...sn) | Combines multiple strings such as s1, s2 into a single string |

Combine multiple strings

SELECT CONCAT("SQL ", "Runoob ", "Gooogle ", "Facebook") AS ConcatenatedString;

| | CONCAT_WS(x, s1,s2...sn) | Same as the CONCAT(s1,s2,...) function, but x is added between each string, where x can be a separator |

Combine multiple strings and add a separator:

SELECT CONCAT_WS("-", "SQL", "Tutorial", "is", "fun!")AS ConcatenatedString;

| | FIELD(s,s1,s2...) | Returns the position of the first string s in the list of strings (s1,s2...) |

Returns the position of the string c in the list of values:

SELECT FIELD("c", "a", "b", "c", "d", "e");

| | FIND_IN_SET(s1,s2) | Returns the position of the string in string s2 that matches s1 |

Returns the position of the string c in the specified string:

SELECT FIND_IN_SET("c", "a,b,c,d,e");

| | FORMAT(x,n) | Formats the number x as "#,###.##", keeping x to n decimal places, with the last digit rounded. |

Format a number in the form "#,###.##":

SELECT FORMAT(250500.5634, 2);     -- 输出 250,500.56

| | INSERT(s1,x,len,s2) | Replaces the string of length len starting at position x in s1 with the string s2 |

Replace the 6 characters starting from the first position of the string with runoob:

SELECT INSERT("google.com", 1, 6, "runoob");  -- 输出:runoob.com

| | LOCATE(s1,s) | Gets the starting position of s1 in the string s |

Get the position of b in the string abc:

SELECT LOCATE('st','myteststring');  -- 5

Returns the position of b in the string abc:

SELECT LOCATE('b', 'abc') -- 2
| | LCASE(s) | Converts all letters of the string s to lowercase |

Convert the string RUNOOB to lowercase:

SELECT LCASE('RUNOOB') -- runoob

| | LEFT(s,n) | Returns the first n characters of the string s |

Returns the first two characters of the string runoob:

SELECT LEFT('runoob',2) -- ru

| | LOWER(s) | Converts all letters of the string s to lowercase |

Convert the string RUNOOB to lowercase:

SELECT LOWER('RUNOOB') -- runoob

| | LPAD(s1,len,s2) | Pads the string s2 at the beginning of string s1 so that the string length reaches len |

Pad the string xx to the beginning of the string abc:

SELECT LPAD('abc',5,'xx') -- xxabc

| | LTRIM(s) | Removes the spaces at the beginning of the string s |

Remove the spaces at the beginning of the string RUNOOB:

SELECT LTRIM("    RUNOOB") AS LeftTrimmedString;-- RUNOOB

| | MID(s,n,len) | Extracts a substring of length len from position n of the string s, same as SUBSTRING(s,n,len) |

Extract 3 characters starting from the 2nd position of the string RUNOOB:

SELECT MID("RUNOOB", 2, 3) AS ExtractString; -- UNO

| | POSITION(s1 IN s) | Gets the starting position of s1 in the string s |

Returns the position of b in the string abc:

SELECT POSITION('b' in 'abc') -- 2

| | REPEAT(s,n) | Repeats the string s n times |

Repeat the string runoob three times:

SELECT REPEAT('runoob',3) -- runoobrunoobrunoob

| | REPLACE(s,s1,s2) | Replaces the string s1 in string s with the string s2 |

Replace the character a in the string abc with the character x:

SELECT REPLACE('abc','a','x') --xbc

| | REVERSE(s) | Reverses the order of the string s |

Reverse the order of the string abc:

SELECT REVERSE('abc') -- cba

| | RIGHT(s,n) | Returns the last n characters of the string s |

Returns the last two characters of the string runoob:

SELECT RIGHT('runoob',2) -- ob

| | RPAD(s1,len,s2) | Appends the string s2 at the end of string s1 so that the string length reaches len |

Pad the string xx to the end of the string abc:

SELECT RPAD('abc',5,'xx') -- abcxx

| | RTRIM(s) | Removes the spaces at the end of the string s |

Remove the trailing spaces of the string RUNOOB:

SELECT RTRIM("RUNOOB     ") AS RightTrimmedString;   -- RUNOOB

| | SPACE(n) | Returns n spaces |

Returns 10 spaces:

SELECT SPACE(10);

| | STRCMP(s1,s2) | Compares strings s1 and s2; returns 0 if s1 equals s2, returns 1 if s1>s2, and if s1Compare strings:

SELECT STRCMP("runoob", "runoob");  -- 0

| | SUBSTR(s, start, length) | Extracts a substring of length length from position start of the string s |

Extract 3 characters starting from the 2nd position of the string RUNOOB:

SELECT SUBSTR("RUNOOB", 2, 3) AS ExtractString; -- UNO

| | SUBSTRING(s, start, length) | Extracts a substring of length length from position start of the string s, equivalent to SUBSTR(s, start, length) |

Extract 3 characters starting from the 2nd position of the string RUNOOB:

SELECT SUBSTRING("RUNOOB", 2, 3) AS ExtractString; -- UNO

| | SUBSTRING_INDEX(s, delimiter, number) | Returns the substring after the number-th occurrence of the delimiter delimiter in the string s.
If number is positive, returns the string to the left of the number-th character.
If number is negative, returns the string to the right of the number-th character (the absolute value of number, counted from the right). |
SELECT SUBSTRING_INDEX('ab','',1) -- a

SELECT SUBSTRING_INDEX('ab','',-1)    -- b

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('abcde','',3),'',-1)    -- c

| | TRIM(s) | Removes the spaces at the beginning and end of the string s |

Remove the leading and trailing spaces of the string RUNOOB:

SELECT TRIM('    RUNOOB    ') AS TrimmedString;

| | UCASE(s) | Converts a string to uppercase |

Convert the string runoob to uppercase:

SELECT UCASE("runoob"); -- RUNOOB

| | UPPER(s) | Converts a string to uppercase |

Convert the string runoob to uppercase:

SELECT UPPER("runoob"); -- RUNOOB

|


MySQL Numeric Functions

Function name Description Example
ABS(x) Returns the absolute value of x

Returns the absolute value of -1:

SELECT ABS(-1) -- returns 1 | | ACOS(x) | Returns the arccosine of x (in radians), where x is a numeric value |

SELECT ACOS(0.25);

| | ASIN(x) | Returns the arcsine (in radians), where x is a numeric value |
SELECT ASIN(0.25);

| | ATAN(x) | Returns the arctangent (in radians), where x is a numeric value |
SELECT ATAN(2.5);

| | ATAN2(n, m) | Returns the arctangent (in radians) |
SELECT ATAN2(-0.8, 2);

| | AVG(expression) | Returns the average value of an expression, where expression is a field |

Returns the average value of the Price field in the Products table:

SELECT AVG(Price) AS AveragePrice FROM Products;

| | CEIL(x) | Returns the smallest integer greater than or equal to x |
SELECT CEIL(1.5) -- 返回2

| | CEILING(x) | Returns the smallest integer greater than or equal to x |
SELECT CEILING(1.5); -- 返回2

| | COS(x) | Returns the cosine value (argument in radians) |
SELECT COS(2);

| | COT(x) | Returns the cotangent value (argument in radians) |
SELECT COT(6);

| | COUNT(expression) | Returns the total number of records in a query; the expression parameter is a field or the * symbol |

Returns how many records there are in the products field of the Products table:

SELECT COUNT(ProductID) AS NumberOfProducts FROM Products;

| | DEGREES(x) | Converts radians to degrees |
SELECT DEGREES(3.1415926535898) -- 180

| | n DIV m | Integer division, where n is the dividend and m is the divisor |

Calculate 10 divided by 5:

SELECT 10 DIV 5;  -- 2

| | EXP(x) | Returns e raised to the power of x |

Calculate e raised to the power of 3:

SELECT EXP(3) -- 20.085536923188

| | FLOOR(x) | Returns the largest integer less than or equal to x |

The integer less than or equal to 1.5:

SELECT FLOOR(1.5) -- 返回1

| | GREATEST(expr1, expr2, expr3, ...) | Returns the largest value in a list |

Returns the largest value in the following list of numbers:

SELECT GREATEST(3, 12, 34, 8, 25); -- 34

Returns the largest value in the following list of strings:

SELECT GREATEST("Google", "Runoob", "Apple");   -- Runoob

| | LEAST(expr1, expr2, expr3, ...) | Returns the smallest value in a list |

Returns the smallest value in the following list of numbers:

SELECT LEAST(3, 12, 34, 8, 25); -- 3

Returns the smallest value in the following list of strings:

SELECT LEAST("Google", "Runoob", "Apple");   -- Apple

| | LN | Returns the natural logarithm of a number, with base e. |

Returns the natural logarithm of 2:

SELECT LN(2);  -- 0.6931471805599453

| | LOG(x) or LOG(base, x) | Returns the natural logarithm (logarithm with base e). If the base parameter is provided, base is the specified base. |
SELECT LOG(20.085536923188) -- 3

SELECT LOG(2, 4); -- 2

| | LOG10(x) | Returns the logarithm with base 10 |
SELECT LOG10(100) -- 2
| | LOG2(x) | Returns the logarithm with base 2 |

Returns the logarithm of 6 with base 2:

SELECT LOG2(6);  -- 2.584962500721156

| | MAX(expression) | Returns the largest value in the field expression |

Returns the largest value of the Price field in the Products table:

SELECT MAX(Price) AS LargestPrice FROM Products;

| | MIN(expression) | Returns the smallest value in the field expression |

Returns the smallest value of the Price field in the Products table:

SELECT MIN(Price) AS MinPrice FROM Products;

| | MOD(x,y) | Returns the remainder after x is divided by y |

The remainder of 5 divided by 2:

SELECT MOD(5,2) -- 1

| | PI() | Returns the value of pi (3.141593) |
SELECT PI() --3.141593

| | POW(x,y) | Returns x raised to the power of y |

2 raised to the power of 3:

SELECT POW(2,3) -- 8

| | POWER(x,y) | Returns x raised to the power of y |

2 raised to the power of 3:

SELECT POWER(2,3) -- 8

| | RADIANS(x) | Converts degrees to radians |

Convert 180 degrees to radians:

SELECT RADIANS(180) -- 3.1415926535898

| | RAND() | Returns a random number between 0 and 1 |
SELECT RAND() --0.93099315644334

| | ROUND(x [,y]) | Returns the integer nearest to x. The optional parameter y indicates the number of decimal places to round to; if omitted, an integer is returned. |
SELECT ROUND(1.23456) --1

SELECT ROUND(345.156, 2) -- 345.16

| | SIGN(x) | Returns the sign of x: -1, 0, and 1 for negative, zero, and positive x, respectively |
SELECT SIGN(-10) -- (-1)

| | SIN(x) | Returns the sine value (argument in radians) |
SELECT SIN(RADIANS(30)) -- 0.5

| | SQRT(x) | Returns the square root of x |

The square root of 25:

SELECT SQRT(25) -- 5

| | SUM(expression) | Returns the sum of the specified field |

Calculate the sum of the Quantity field in the OrderDetails table:

SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;

| | TAN(x) | Returns the tangent value (argument in radians) |
SELECT TAN(1.75);  -- -5.52037992250933

| | TRUNCATE(x,y) | Returns the value of x truncated to y decimal places (the biggest difference from ROUND is that no rounding is performed) |
SELECT TRUNCATE(1.23456,3) -- 1.234

|


MySQL Date Functions

Function name Description Example
ADDDATE(d,n) Calculates the date of the starting date d plus n days

SELECT ADDDATE("2017-06-15", INTERVAL 10 DAY); ->2017-06-25 | | ADDTIME(t,n) | n is a time expression; adds the time expression n to the time t |

Add 5 seconds:

SELECT ADDTIME('2011-11-11 11:11:11', 5);

->2011-11-11 11:11:16 (秒)

Add 2 hours, 10 minutes, 5 seconds:

SELECT ADDTIME("2020-06-15 09:34:21", "2:10:5");

-> 2020-06-15 11:44:26

| | CURDATE() | Returns the current date |
SELECT CURDATE();

-> 2018-09-19

| | CURRENT_DATE() | Returns the current date |
SELECT CURRENT_DATE();

-> 2018-09-19

| | CURRENT_TIME | Returns the current time |
SELECT CURRENT_TIME();

-> 19:59:02

| | CURRENT_TIMESTAMP() | Returns the current date and time |
SELECT CURRENT_TIMESTAMP()

-> 2018-09-19 20:57:43

| | CURTIME() | Returns the current time |
SELECT CURTIME();

-> 19:59:02

| | DATE() | Extracts the date value from a date or datetime expression |
SELECT DATE("2017-06-15");
-> 2017-06-15
| | DATEDIFF(d1,d2) | Calculates the number of days between dates d1->d2 |
SELECT DATEDIFF('2001-01-01','2001-02-02')

-> -32

| | DATE_ADD(d, INTERVAL expr type) | Calculates the date of the starting date d plus a time interval; the type value can be:

  • 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 DATE_ADD("2017-06-15", INTERVAL 10 DAY);    

-> 2017-06-25

SELECT DATE_ADD("2017-06-15 09:34:21", INTERVAL 15 MINUTE); -> 2017-06-15 09:49:21

SELECT DATE_ADD("2017-06-15 09:34:21", INTERVAL -3 HOUR); ->2017-06-15 06:34:21

SELECT DATE_ADD("2017-06-15 09:34:21", INTERVAL -3 MONTH); ->2017-03-15 09:34:21

| | DATE_FORMAT(d,f) | Displays the date d according to the requirements of the expression f |
SELECT DATE_FORMAT('2011-11-11 11:11:11','%Y-%m-%d %r')

-> 2011-11-11 11:11:11 AM

| | DATE_SUB(date,INTERVAL expr type) | Subtracts the specified time interval from a date. |

Subtract 2 days from the OrderDate field in the Orders table:

SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 2 DAY) AS OrderPayDate

FROM Orders

| | DAY(d) | Returns the day part of the date value d |
SELECT DAY("2017-06-15");
-> 15
| | DAYNAME(d) | Returns the day of the week for the date d, such as Monday, Tuesday |
SELECT DAYNAME('2011-11-11 11:11:11')

->Friday

| | DAYOFMONTH(d) | Calculates which day of the month the date d is |
SELECT DAYOFMONTH('2011-11-11 11:11:11')

->11

| | DAYOFWEEK(d) | Returns the day of the week for the date d: 1 for Sunday, 2 for Monday, and so on |
SELECT DAYOFWEEK('2011-11-11 11:11:11')

->6

| | DAYOFYEAR(d) | Calculates which day of the year the date d is |
SELECT DAYOFYEAR('2011-11-11 11:11:11')

->315

| | EXTRACT(type FROM d) | Gets the specified value from the date d; type specifies the value to return.
type can take the following values:

  • 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-11-11 11:11:11') 

-> 11

| | FROM_DAYS(n) | Calculate the date n days after January 1, 0000 |
SELECT FROM_DAYS(1111)

-> 0003-01-16

| | HOUR(t) | Return the hour value in t |
SELECT HOUR('1:2:3')

-> 1

| | LAST_DAY(d) | Return the last day of the month for a given date |
SELECT LAST_DAY("2017-06-20");

-> 2017-06-30

| | LOCALTIME() | Return the current date and time |
SELECT LOCALTIME()

-> 2018-09-19 20:57:43

| | LOCALTIMESTAMP() | Return the current date and time |
SELECT LOCALTIMESTAMP()

-> 2018-09-19 20:57:43

| | MAKEDATE(year, day-of-year) | Return a date based on the given year parameter and the day-of-year number |
SELECT MAKEDATE(2017, 3);

-> 2017-01-03

| | MAKETIME(hour, minute, second) | Combine time; the parameters are hour, minute, and second |
SELECT MAKETIME(11, 35, 4);

-> 11:35:04

| | MICROSECOND(date) | Return the microseconds corresponding to the date argument |
SELECT MICROSECOND("2017-06-20 09:34:00.000023");

-> 23

| | MINUTE(t) | Return the minute value in t |
SELECT MINUTE('1:2:3')

-> 2

| | MONTHNAME(d) | Return the month name in a date, such as November |
SELECT MONTHNAME('2011-11-11 11:11:11')

-> November

| | MONTH(d) | Return the month value in date d, from 1 to 12 |
SELECT MONTH('2011-11-11 11:11:11')

->11

| | NOW() | Return the current date and time |
SELECT NOW()

-> 2018-09-19 20:57:43

| | PERIOD_ADD(period, number) | Add a period to a year-month combined date |
SELECT PERIOD_ADD(201703, 5);   

-> 201708

| | PERIOD_DIFF(period1, period2) | Return the difference in months between two periods |
SELECT PERIOD_DIFF(201710, 201703);

-> 7

| | QUARTER(d) | Return which quarter date d falls in, from 1 to 4 |
SELECT QUARTER('2011-11-11 11:11:11')

-> 4

| | SECOND(t) | Return the seconds value in t |
SELECT SECOND('1:2:3')

-> 3

| | SEC_TO_TIME(s) | Convert time s in seconds to the hours-minutes-seconds format |
SELECT SEC_TO_TIME(4320)

-> 01:12:00

| | STR_TO_DATE(string, format_mask) | Convert a string into a date |
SELECT STR_TO_DATE("August 10 2017", "%M %d %Y");

-> 2017-08-10

| | SUBDATE(d,n) | Return the date n days before date d |
SELECT SUBDATE('2011-11-11 11:11:11', 1)

->2011-11-10 11:11:11 (默认是天)

| | SUBTIME(t,n) | Return the time n seconds before time t |
SELECT SUBTIME('2011-11-11 11:11:11', 5)

->2011-11-11 11:11:06 (秒)

| | SYSDATE() | Return the current date and time |
SELECT SYSDATE()

-> 2018-09-19 20:57:43

| | TIME(expression) | Extract the time part of the given expression |
SELECT TIME("19:30:10");

-> 19:30:10

| | TIME_FORMAT(t,f) | Display time t according to the format specified by expression f |
SELECT TIME_FORMAT('11:11:11','%r')

11:11:11 AM

| | TIME_TO_SEC(t) | Convert time t to seconds |
SELECT TIME_TO_SEC('1:12:00')

-> 4320

| | TIMEDIFF(time1, time2) | Calculate the difference between two times |
mysql> SELECT TIMEDIFF("13:10:11", "13:10:10");

-> 00:00:01

mysql> SELECT TIMEDIFF('2000:01:01 00:00:00',

    ->                 '2000:01:01 00:00:00.000001');

        -> '-00:00:00.000001'

mysql> SELECT TIMEDIFF('2008-12-31 23:59:59.000001',

    ->                 '2008-12-30 01:01:01.000002');

        -> '46:58:57.999999'

| | TIMESTAMP(expression, interval) | With a single argument, the function returns a date or datetime expression; with two arguments, the arguments are added together |
mysql> SELECT TIMESTAMP("2017-07-23",  "13:10:11");

-> 2017-07-23 13:10:11

mysql> SELECT TIMESTAMP('2003-12-31');

        -> '2003-12-31 00:00:00'

mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');

        -> '2004-01-01 00:00:00'

| | TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2) | Calculate the time difference, returning the difference of datetime_expr2 − datetime_expr1 |
mysql> SELECT TIMESTAMPDIFF(DAY,'2003-02-01','2003-05-01');   // 计算两个时间相隔多少天

        -> 89

mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');   // 计算两个时间相隔多少月

        -> 3

mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');    // 计算两个时间相隔多少年

        -> -1

mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');  // 计算两个时间相隔多少分钟

        -> 128885

| | TO_DAYS(d) | Calculate the number of days from date d to January 1, 0000 |
SELECT TO_DAYS('0001-01-01 01:01:01')

-> 366

| | WEEK(d) | Calculate which week of the year date d falls in, ranging from 0 to 53 |
SELECT WEEK('2011-11-11 11:11:11')

-> 45

| | WEEKDAY(d) | The day of the week for date d, where 0 is Monday and 1 is Tuesday |
SELECT WEEKDAY("2017-06-15");

-> 3

| | WEEKOFYEAR(d) | Calculate which week of the year date d falls in, ranging from 0 to 53 |
SELECT WEEKOFYEAR('2011-11-11 11:11:11')

-> 45

| | YEAR(d) | Return the year |
SELECT YEAR("2017-06-15");

-> 2017

| | YEARWEEK(date, mode) | Return the year and week number (0 to 53); in mode, 0 means Sunday, 1 means Monday, and so on |
SELECT YEARWEEK("2017-06-15");

-> 201724

|

MySQL Advanced Functions

Function name Description Example
BIN(x) Return the binary encoding of x, where x is a decimal number

Binary encoding of 15:

SELECT BIN(15); -- 1111 | | BINARY(s) | Convert the string s to a binary string |

SELECT BINARY "RUNOOB";

-> RUNOOB

| |
CASE expression

WHEN condition1 THEN result1

WHEN condition2 THEN result2

...

WHEN conditionN THEN resultN

ELSE result

END
| CASE marks the start of the function and END marks its end. If condition1 is true, result1 is returned; if condition2 is true, result2 is returned; if none are true, result is returned. Once one condition is satisfied, the ones after it are not executed. |
SELECT CASE

  WHEN 1 > 0

  THEN '1 > 0'

  WHEN 2 > 0

  THEN '2 > 0'

  ELSE '3 > 0'

  END

->1 > 0

| | CAST(x AS type) | Convert the data type |

Convert a date string to a date:

SELECT CAST("2017-08-29" AS DATE);

-> 2017-08-29

| | COALESCE(expr1, expr2, ...., expr_n) | Return the first non-null expression among the arguments (from left to right) |
SELECT COALESCE(NULL, NULL, NULL, 'runoob.com', NULL, 'google.com');

-> runoob.com

| | CONNECTION_ID() | Return the unique connection ID |
SELECT CONNECTION_ID();

-> 4292835

| | CONV(x,f1,f2) | Return the number converted from base f1 to base f2 |
SELECT CONV(15, 10, 2);

-> 1111

| | CONVERT(s USING cs) | Change the character set of string s to cs |
SELECT CHARSET('ABC')

->utf-8

SELECT CHARSET(CONVERT('ABC' USING gbk)) ->gbk

| | CURRENT_USER() | Return the current user |
SELECT CURRENT_USER();

-> guest@%

| | DATABASE() | Return the current database name |
SELECT DATABASE();
-> runoob
| | IF(expr,v1,v2) | If expression expr is true, return v1; otherwise, return v2. |
SELECT IF(1 > 0,'正确','错误')
->正确
| | IFNULL(v1,v2) | If the value of v1 is not NULL, return v1; otherwise, return v2. |
SELECT IFNULL(null,'Hello Word')

->Hello Word

| | ISNULL(expression) | Determine whether the expression is NULL |
SELECT ISNULL(NULL);

->1

| | LAST_INSERT_ID() | Return the most recently generated AUTO_INCREMENT value |
SELECT LAST_INSERT_ID();

->6

| | NULLIF(expr1, expr2) | Compare two strings; if expr1 equals expr2, return NULL; otherwise, return expr1 |
SELECT NULLIF(25, 25);

->

| | SESSION_USER() | Return the current user |
SELECT SESSION_USER();

-> guest@%

| | SYSTEM_USER() | Return the current user |
SELECT SYSTEM_USER();

-> guest@%

| | USER() | Return the current user |
SELECT USER();

-> guest@%

| | VERSION() | Return the database version number |
SELECT VERSION()

-> 5.6.34

|

The following are some commonly used functions added in MySQL 8.0:

function Description Example
JSON_OBJECT() Convert key-value pairs into a JSON object
SELECT JSON_OBJECT('key1', 'value1', 'key2', 'value2')
JSON_ARRAY() Convert values into a JSON array
SELECT JSON_ARRAY(1, 2, 'three')
JSON_EXTRACT() Extract a specified value from a JSON string
SELECT JSON_EXTRACT('{"name": "John", "age": 30}', '$.name')
JSON_CONTAINS() Check whether a JSON string contains a specified value
SELECT JSON_CONTAINS('{"name": "John", "age": 30}', 'John', '$.name')
ROW_NUMBER() Assign a unique number to each row in the query result
SELECT ROW_NUMBER() OVER(ORDER BY id) AS row_number, name FROM users
RANK() Assign a rank to each row in the query result
SELECT RANK() OVER(ORDER BY score DESC) AS rank, name, score FROM students