目录
以下是100条非常有用的MySQL SQL语句,涵盖了常见的查询、数据操作、性能优化和高级功能等各方面的使用:
一、基本查询
1.1 选择所有记录:
SELECT * FROM table_name;
1.2 选择特定列:
SELECT column1, column2 FROM table_name;
1.3 过滤记录:
SELECT * FROM table_name WHERE condition;
1.4 排序记录:
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
1.5 限制记录数:
SELECT * FROM table_name LIMIT 10;
1.6 统计记录数:
SELECT COUNT(*) FROM table_name;
1.7 求和:
SELECT SUM(column_name) FROM table_name;
1.8 平均值:
SELECT AVG(column_name) FROM table_name;
1.9 最大值:
SELECT MAX(column_name) FROM table_name;
1.10 最小值:
SELECT MIN(column_name) FROM table_name;
二、多表查询
2.1 内连接:
SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;
2.2 左连接:
SELECT * FROM table1
LEFT JOIN table2 ON table1.column = table2.column;
2.3 右连接:
SELECT * FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
2.4 全连接:
SELECT * FROM table1
FULL OUTER JOIN table2 ON table1.column = table2.column;
2.5 子查询:
SELECT * FROM table_name
WHERE column_name
IN (SELECT column_name FROM another_table);
2.6 交叉连接:
SELECT * FROM table1 CROSS JOIN table2;
2.7 自连接:
SELECT * FROM table_name AS a, table_name AS b WHERE a.column = b.column;
三、数据操作
3.1 插入记录:
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
3.2 批量插入:
INSERT INTO table_name (column1, column2)
VALUES (value1, value2), (value3, value4);
3.3 更新记录:
UPDATE table_name SET column1 = value1, column2 = value2
WHERE condition;
3.4 删除记录:
DELETE FROM table_name WHERE condition;
3.5 替换记录:
REPLACE INTO table_name (column1, column2) VALUES (value1, value2);
3.6 插入选择记录:
INSERT INTO table_name1 (column1, column2)
SELECT column1, column2 FROM table_name2 WHERE condition;
3.7 按条件删除:
DELETE FROM table_name WHERE condition;
3.8 清空表:
TRUNCATE TABLE table_name;
四、索引和视图
4.1 创建索引:
CREATE INDEX index_name ON table_name (column1, column2);
4.2 删除索引:
DROP INDEX index_name ON table_name;
4.3 创建视图:
CREATE VIEW view_name AS SELECT column1, column2
FROM table_name WHERE condition;
4.4 删除视图:
DROP VIEW view_name;
五、存储过程和触发器
5.1 创建存储过程:
CREATE PROCEDURE procedure_name (IN parameter INT)
BEGIN
-- SQL Statements
END;
5.2 调用存储过程:
CALL procedure_name(parameter);
5.3 创建触发器:
CREATE TRIGGER trigger_name BEFORE|AFTER INSERT|UPDATE|DELETE ON table_name
FOR EACH ROW
BEGIN
-- SQL Statements
END;
六、高级查询
6.1 分组统计:
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;
6.2 分组过滤:
SELECT column_name, COUNT(*) FROM table_name
GROUP BY column_name HAVING COUNT(*) > 1;
6.3 分页查询:
SELECT * FROM table_name LIMIT 10 OFFSET 20;
6.4 联合查询:
SELECT column_name FROM table1 UNION SELECT column_name FROM table2;
6.5 联合查询并去重:
SELECT column_name FROM table1
UNION DISTINCT SELECT column_name FROM table2;
6.6 递归查询:
WITH RECURSIVE cte AS (
SELECT column FROM table WHERE condition
UNION ALL
SELECT column FROM table, cte WHERE condition
)
SELECT * FROM cte;
6.7 窗口函数:
SELECT column_name,
ROW_NUMBER() OVER (PARTITION BY column ORDER BY column) AS row_num
FROM table_name;
七、性能优化
7.1 分析查询:
EXPLAIN SELECT * FROM table_name;
7.2 查看索引:
SHOW INDEX FROM table_name;
7.3 创建复合索引:
CREATE INDEX index_name ON table_name (column1, column2);
7.4 删除复合索引:
DROP INDEX index_name ON table_name;
八、数据库管理
8.1 创建数据库:
CREATE DATABASE database_name;
8.2 删除数据库:
DROP DATABASE database_name;
8.3 备份数据库:
mysqldump -u username -p database_name > backup_file.sql;
8.4 恢复数据库:
mysql -u username -p database_name < backup_file.sql;
九、字符串操作
9.1 字符串长度:
SELECT LENGTH(column_name) FROM table_name;
9.2 字符串拼接:
SELECT CONCAT(column1, column2) FROM table_name;
9.3 子字符串:
SELECT SUBSTRING(column_name, start, length) FROM table_name;
9.4 替换字符串:
SELECT REPLACE(column_name, 'old_string', 'new_string') FROM table_name;
9.5 字符串转大写:
SELECT UPPER(column_name) FROM table_name;
9.6 字符串转小写:
SELECT LOWER(column_name) FROM table_name;
十、日期时间操作
10.1 当前日期和时间:
SELECT NOW();
10.2 当前日期:
SELECT CURDATE();
10.3 当前时间:
SELECT CURTIME();
10.4 日期加天数:
SELECT DATE_ADD(NOW(), INTERVAL 10 DAY);
10.5 日期减天数:
SELECT DATE_SUB(NOW(), INTERVAL 10 DAY);
10.6 日期差:
SELECT DATEDIFF(date1, date2);
10.7 提取年:
SELECT YEAR(date_column) FROM table_name;
10.8 提取月:
SELECT MONTH(date_column) FROM table_name;
10.9 提取日:
SELECT DAY(date_column) FROM table_name;
10.10 提取小时:
SELECT HOUR(time_column) FROM table_name;
10.11 提取分钟:
SELECT MINUTE(time_column) FROM table_name;
10.12提取秒:
SELECT SECOND(time_column) FROM table_name;
十一、数据类型转换
11.1 整数转字符串:
SELECT CAST(column_name AS CHAR) FROM table_name;
11.2 字符串转整数:
SELECT CAST(column_name AS UNSIGNED) FROM table_name;
11.3 字符串转日期:
SELECT CAST(column_name AS DATE) FROM table_name;
11.4 日期转字符串:
SELECT DATE_FORMAT(date_column, '%Y-%m-%d') FROM table_name;
11.5 字符串转时间:
SELECT CAST(column_name AS TIME) FROM table_name;
十二、安全和权限
12.1 创建用户:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
12.2 删除用户:
DROP USER 'username'@'host';
12.3 授予权限:
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host';
12.4 撤销权限:
REVOKE ALL PRIVILEGES ON database_name.* FROM 'username'@'host';
12.5 显示用户权限:
SHOW GRANTS FOR 'username'@'host';
12.6 刷新权限:
FLUSH PRIVILEGES;
十三、JSON操作
13.1 创建JSON列:
CREATE TABLE table_name (
id INT,
data JSON
);
13.2 插入JSON数据:
INSERT INTO table_name (id, data)
VALUES (1, '{"key1": "value1", "key2": "value2"}');
13.3 查询JSON数据:
SELECT JSON_EXTRACT(data, '$.key1') FROM table_name;
13.4 更新JSON数据:
UPDATE table_name
SET data = JSON_SET(data, '$.key1', 'new_value')
WHERE id = 1;
13.5 删除JSON键值:
UPDATE table_name SET data = JSON_REMOVE(data, '$.key1') WHERE id = 1;
十四、其他有用的语句
14.1 查询表结构:
DESCRIBE table_name;
14.2 查询数据库中的所有表:
SHOW TABLES;
14.3 查询表的创建语句:
SHOW CREATE TABLE table_name;
14.4 查询数据库的所有列:
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'table_name';
14.5 查询表中的所有索引:
SHOW INDEX FROM table_name;
14.6 查询表的大小:
SELECT table_name AS 'Table',
ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2) AS 'Size (MB)'
FROM information_schema.TABLES WHERE table_schema = 'database_name';
14.7 设置时区:
--设置亚洲/上海时区
SET time_zone = 'Asia/Shanghai';
十五、锁和事务
15.1 开启事务:
START TRANSACTION;
15.2 提交事务:
COMMIT;
15.3 回滚事务:
ROLLBACK;
15.4 表锁定:
LOCK TABLES table_name READ|WRITE;
15.5 表解锁:
UNLOCK TABLES;
十六、性能调优
16.1 添加列的默认值:
ALTER TABLE table_name ALTER column_name SET DEFAULT 'default_value';
16.2 删除列的默认值:
ALTER TABLE table_name ALTER column_name DROP DEFAULT;
16.3 重命名表:
RENAME TABLE old_table_name TO new_table_name;
16.4 重命名列:
ALTER TABLE table_name CHANGE old_column_name new_column_name datatype;
16.5 修改列类型:
ALTER TABLE table_name MODIFY column_name new_datatype;
16.6 添加新列:
ALTER TABLE table_name ADD column_name datatype;
16.7 删除列:
ALTER TABLE table_name DROP COLUMN column_name;
16.8 优化表:
OPTIMIZE TABLE table_name;
以上是100条常用且有用的MySQL SQL语句,它们涵盖了数据库的各个方面,从基本查询到高级操作和性能优化,适用于各种实际应用场景。