MySQL中的常用函数主要用于数据处理、字符串操作、日期和时间处理、数学计算等方面。以下是一些常用函数及其作用:
-- 字符串函数示例
SELECT CONCAT('Hello', ' ', 'World') AS greeting;
SELECT LENGTH('Hello World') AS length;
SELECT SUBSTRING('Hello World', 1, 5) AS substring;
SELECT UPPER('hello world') AS upper;
SELECT LOWER('HELLO WORLD') AS lower;
SELECT REPLACE('Hello World', 'World', 'MySQL') AS replaced;
-- 数学函数示例
SELECT ABS(-10) AS absolute;
SELECT RAND() AS random_number;
SELECT ROUND(3.14159, 2) AS rounded;
SELECT CEILING(3.14) AS ceiling;
SELECT FLOOR(3.14) AS floor;
-- 日期和时间函数示例
SELECT NOW() AS current_datetime;
SELECT CURDATE() AS current_date;
SELECT CURTIME() AS current_time;
SELECT DATE_ADD('2023-01-01', INTERVAL 1 MONTH) AS next_month;
SELECT DATEDIFF('2023-02-01', '2023-01-01') AS days_diff;
SELECT YEAR('2023-01-01') AS year;
SELECT MONTH('2023-01-01') AS month;
SELECT DAY('2023-01-01') AS day;
-- 聚合函数示例
SELECT COUNT(*) AS total_rows FROM users;
SELECT SUM(salary) AS total_salary FROM employees;
SELECT AVG(age) AS average_age FROM customers;
SELECT MAX(price) AS max_price FROM products;
SELECT MIN(quantity) AS min_quantity FROM orders;
-- 类型转换函数示例
SELECT CAST('123' AS INT) AS int_value;
SELECT CONVERT('2023-01-01', DATE) AS date_value;
领取专属 10元无门槛券
手把手带您无忧上云