Instructions for using MySQL built-in functions

MySQL function instructions

insert image description here

MySQL is a popular relational database management system that provides many built-in functions to process and manipulate data. These functions can simplify the process of database query and operation, and improve the readability and efficiency of the code. Following are some common MySQL built-in functions with descriptions and examples of their usage.

Numeric function

ABS()

Function prototype: ABS(x)
returns the absolute value of the parameter x.

Example:

SELECT ABS(-5); -- 返回 5

ROUND(x, reserved digits)

Function prototype: ROUND(x, d)
round the parameter x to the specified number of reserved digits d.

Example:

SELECT ROUND(3.14159, 2);  -- 返回 3.14

CEIL(x)

Function prototype: CEIL(x)
returns the smallest integer greater than or equal to the parameter x.

Example:

SELECT CEIL(3.7); -- 返回 4

FLOOR(x)

Function prototype: FLOOR(x)
returns the largest integer less than or equal to the parameter x.

Example:

SELECT FLOOR(3.7); -- 返回 3

TRUNCATE(x, D)

Function prototype: TRUNCATE(x, D)
truncate the parameter x to the specified number of decimal places D.

Example:

SELECT TRUNCATE(3.14159, 2); -- 返回 3.14

conditional function

IF()

Function prototype: IF(condition, true_value, false_value)
returns different values ​​according to condition.

Example:

SELECT IF(10 > 5, 'true', 'false'); -- 返回 'true'

IFNULL()

Function prototype: IFNULL(expression, value)
If expression is not NULL, return the value of expression, otherwise return value.

Example:

SELECT IFNULL(NULL, 'not null'); -- 返回 'not null'

String functions

CONCAT()

Function prototype: CONCAT(string1, string2, ..., stringN)
Concatenate multiple strings into one string.

Example:

SELECT CONCAT('Hello', ' ', 'World'); -- 返回 'Hello World'

CONCAT_WS()

Function prototype: CONCAT_WS(separator, string1, string2, ..., stringN)
concatenate multiple strings into one string, separated by separator.

Example:

SELECT CONCAT_WS(',', 'apple', 'banana', 'orange'); -- 返回 'apple,banana,orange'

LENGTH()

Function prototype: LENGTH(string)
returns the length of the string.

Example:

SELECT LENGTH('Hello'); -- 返回 5

LEFT()

Function prototype: LEFT(string, length)
returns the substring of the specified length on the left side of the string.

Example:

SELECT LEFT('Hello', 3); -- 返回 'Hel'

RIGHT()

Function prototype: RIGHT(string, length)
returns the substring of the specified length on the right side of the string.

Example:

SELECT RIGHT('Hello', 3); -- 返回 'llo'

MID()

Function prototype: MID(string, start, length)
returns a substring of the specified length from the specified position of the string.

Example:

SELECT MID('Hello', 2, 3); -- 返回 'ell'

REPLACE()

Function prototype: REPLACE(string, old_text, new_text)
replace a substring in a string.

Example:

SELECT REPLACE('Hello World', 'World', 'Bob'); -- 返回 'Hello Bob'

SUBSTRING()

Function prototype: SUBSTRING(string, start, length)
returns a substring of the specified length from the specified position of the string.

Example:

SELECT SUBSTRING('Hello', 2, 3); -- 返回 'ell'

SUBSTRING_INDEX()

Function prototype: SUBSTRING_INDEX(string, delimiter, count)
returns the occurrence position of the specified substring in the string, and returns the substring.

Example:

SELECT SUBSTRING_INDEX('www.example.com', '.', 2); -- 返回 'www.example'

aggregate function

COUNT()

Function Prototype: COUNT(expression)
Returns the number of rows in the table.

Example:

SELECT COUNT(*) FROM users; -- 返回 users 表的行数

COUNT_BIG()

Function prototype: COUNT_BIG(expression)
returns the number of rows in the table (without limiting the size of the result set).

Example:

SELECT COUNT_BIG(*) FROM users; -- 返回 users 表的行数

SUM()

Function prototype: SUM(expression)
returns the sum of the specified column in the table.

Example:

SELECT SUM(price) FROM orders; -- 返回 orders 表中 price 列的总和

AVG()

Function prototype: AVG(expression)
returns the average value of the specified column in the table.

Example:

SELECT AVG(age) FROM students; -- 返回 students 表中 age 列的平均值

MAX()

Function prototype: MAX(expression)
returns the maximum value of the specified column in the table.

Example:

SELECT MAX(score) FROM exams; -- 返回 exams 表中 score 列的最大值

MIN()

Function prototype: MIN(expression)
returns the minimum value of the specified column in the table.

Example:

SELECT MIN(price) FROM products; -- 返回 products 表中 price 列的最小值

GROUP_CONCAT()

Function Prototype: GROUP_CONCAT(expression, separator)
Concatenates multiple strings and displays them as one row in the result set.

Example:

SELECT GROUP_CONCAT(name, ', ') FROM students; -- 返回 students 表中 name 列的所有值,使用逗号分隔

date and time functions

DATE_FORMAT()

Function prototype: DATE_FORMAT(date, format)
Format date and time strings.

Example:

SELECT DATE_FORMAT(CURRENT_TIMESTAMP, '%Y-%m-%d'); -- 返回当前日期的年、月、日格式

DATE_ADD()

Function prototype: DATE_ADD(date, INTERVAL value unit)
add a date to the specified time interval.

Example:

SELECT DATE_ADD('2021-01-01', INTERVAL 1 MONTH); -- 返回 '2021-02-01'

DATE_SUB()

Function prototype: DATE_SUB(date, INTERVAL value unit)
subtract a specified time interval from a date.

Example:

SELECT DATE_SUB('2021-01-01', INTERVAL 1 DAY); -- 返回 '2020-12-31'

TIME_FORMAT()

Function prototype: TIME_FORMAT(time, format)
format timestamp string.

Example:

SELECT TIME_FORMAT('12:30:45', '%H:%i:%s'); -- 返回 '12:30:45'

UNIX_TIMESTAMP()

Function prototype: UNIX_TIMESTAMP([date])
convert datetime string to Unix timestamp.

Example:

SELECT UNIX_TIMESTAMP('2021-01-01 12:00:00'); -- 返回 Unix 时间戳

YEAR()

Function prototype: YEAR(date)
extract the year part in the date and time string.

Example:

SELECT YEAR('2021-01-01'); -- 返回 2021

MONTH()

Function prototype: MONTH(date)
extract the month part in the datetime string.

Example:

SELECT MONTH('2021-01-01'); -- 返回 1

DAY()

Function prototype: DAY(date)
extract the number of days in the date and time string.

Example:

SELECT DAY('2021-01-01'); -- 返回 1

HOUR()

Function prototype: HOUR(time)
extract the hour part of the date and time string.

Example:

SELECT HOUR('12:30:45'); -- 返回 12

MINUTE()

Function prototype: MINUTE(time)
Extract the minute part of the date and time string.

Example:

SELECT MINUTE('12:30:45'); -- 返回 30

SECOND()

Function prototype: SECOND(time)
extract the seconds part in the date and time string.

Example:

SELECT SECOND('12:30:45'); -- 返回 45

The above are descriptions and examples of some commonly used MySQL functions. These functions can help you process and manipulate data better, and play an important role in MySQL statements. Using these functions can improve query efficiency and data processing flexibility. For more detailed descriptions of functions, please refer to the official MySQL documentation.

Guess you like

Origin blog.csdn.net/weixin_45626288/article/details/132067619