常用的SQL语句
下面列举了一些我们在开发中常常会使用到的SQL语句,供大家参考学习。
1. 查询所有数据:SELECT * FROM table_name;
2. 查询指定列数据:SELECT column1, column2 FROM table_name;
3. 带条件查询:SELECT * FROM table_name WHERE condition;
4. 带条件查询并排序:SELECT * FROM table_name WHERE condition ORDER BY column_name ASC/DESC;
5. 带分组的查询:SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
6. 带分组和排序的查询:SELECT column1, COUNT(*) FROM table_name GROUP BY column1 ORDER BY COUNT(*) DESC;
7. 带聚合函数的查询:SELECT AVG(column1), MAX(column2), MIN(column3) FROM table_name;
8. 带子查询的查询:SELECT * FROM table_name WHERE column1 IN (SELECT column1 FROM other_table);
9. 带连接条件的查询:SELECT * FROM table1 JOIN table2 lumn1 = lumn2;
10. 带连接和排序条件的查询:SELECT * FROM table1 JOIN table2 lumn1 = lumn2 ORDER lumn2 ASC;
11. 带连接和分组条件的查询:SELECT * FROM table1 JOIN table2 lumn1 = lumn2 GROUP lumn2;
12. 带连接和聚合函数条件的查询:SELECT * FROM table1 JOIN table2 lumn1 = lumn2 GROUP lumn2 HAVING lumn3) > 0;
常用的sql查询语句有哪些13. 插入数据:INSERT INTO table_name (column1, column2) VALUES (value1, value2);
14. 更新数据:UPDATE table_name SET column1 = value1 WHERE condition;
15. 删除数据:DELETE FROM table_name WHERE condition;
16. 清空表数据:TRUNCATE TABLE table_name;
17. 创建表:CREATE TABLE table_name (column1 datatype, column2 datatype, ...);
18. 修改表结构:ALTER TABLE table_name ADD column_name datatype;
19. 删除表:DROP TABLE table_name;
20. 查看表结构:DESCRIBE table_name;
21. 查看表数据量:SELECT COUNT(*) FROM table_name;
22. 查看表索引:SHOW INDEX FROM table_name;
23. 创建索引:CREATE INDEX index_name ON table_name (column_name);
24. 删除索引:DROP INDEX index_name ON table_name;
25. 批量插入数据:INSERT INTO table_name (column1, column2) VALUES (value1, value2), (value3, value4), ...;
26. 批量更新数据:UPDATE table_name SET column1 = value1 WHERE condition, column2 = value2 WHERE condition, ...;
27. 批量删除数据:DELETE FROM table_name WHERE condition, ...;
28. 分页查询:SELECT * FROM table_name LIMIT offset, limit;
29. 子查询嵌套查询:(SELECT column1 FROM other_table WHERE condition) IN (SELECT column1 FROM another_table WHERE condition);
30. UNION操作符查询:SELECT * FROM table1 UNION SELECT * FROM table2。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论