In database development, functions are a powerful tool that can be used for query optimization, data manipulation, and data transformation. As a popular relational database, MySQL provides a wealth of built-in functions for handling various database operations. This article will give an in-depth introduction to the common function operations of MySQL to help you optimize queries and process data.
Article Directory
1. Aggregation function
COUNT function: used to count the number of records.
SELECT COUNT(*) FROM orders;
SUM function: used for summing.
SELECT SUM(price) FROM products;
AVG function: used to calculate the average value.
SELECT AVG(age) FROM employees;
MAX and MIN functions: used to find the maximum and minimum values respectively.
SELECT MAX(salary) FROM employees;
SELECT MIN(salary) FROM employees;
2. String functions
CONCAT function: used to concatenate multiple strings.
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;
LENGTH function: used to calculate the length of a string.
SELECT LENGTH(description) FROM products;
UPPER and LOWER functions: used to convert to uppercase and lowercase respectively.
SELECT UPPER(username) FROM users;
SELECT LOWER(email) FROM users;
SUBSTRING function: used to intercept substrings.
SELECT SUBSTRING(title, 1, 10) FROM articles;
3. Date function
NOW function: returns the current date and time.
SELECT NOW();
DATE_FORMAT function: used to format the date.
SELECT DATE_FORMAT(order_date, '%Y-%m-%d') FROM orders;
DATEDIFF function: Used to calculate the difference in days between two dates.
SELECT DATEDIFF('2023-07-01', '2023-06-15');
4. Conditional function
IF function: Returns different values based on conditions.
SELECT IF(quantity > 10, 'In Stock', 'Out of Stock') FROM products;
CASE function: perform multi-branch judgment according to conditions.
SELECT
CASE
WHEN age < 18 THEN 'Minor'
WHEN age >= 18 AND age < 65 THEN 'Adult'
ELSE 'Senior'
END AS age_group
FROM persons;
Five, type conversion function
CAST function: perform data type conversion.
SELECT CAST(price AS DECIMAL(10, 2)) FROM products;
CONVERT function: It is also used for data type conversion, but it may be used differently in different database systems.
SELECT CONVERT(price, DECIMAL(10, 2)) FROM products;
6. Grouping function
GROUP_CONCAT function: Concatenates a set of values into a string.
SELECT department, GROUP_CONCAT(name) FROM employees GROUP BY department;
Combination of GROUP BY and aggregate function: used for grouping and aggregate calculation.
SELECT department, AVG(salary) FROM employees GROUP BY department;
MySQL's built-in functions provide powerful tools for query optimization and data manipulation. From aggregate functions, string functions, date functions to conditional functions and type conversion functions, each function has a specific purpose and function. Mastering these commonly used functions can improve efficiency in database development, reduce the amount of code, and better process and convert data. Through in-depth understanding and practice, you can flexibly use these functions in data operations, and write efficient and elegant database query and processing codes.