当前位置:   article > 正文

Mysql常用的查询语法~!(个人收藏)_mysql查询语句

mysql查询语句

MySQL是一个流行的开源关系型数据库管理系统,支持多种查询语法来操作数据库。以下是MySQL常用的查询语法:

1. 查询所有数据:

SELECT * FROM table_name;

2.查询指定字段的数据:

SELECT column1, column2 FROM table_name;

3.使用WHERE子句进行条件查询:

SELECT * FROM table_name WHERE condition;

4.使用ORDER BY 进行排序:

SELECT * FROM table_name ORDER BY column_name [ASC|DESC];

5.使用LIMIT限制查询结果数量:

SELECT * FROM table_name LIMIT num;

6.使用LIMIT和OFFSET进行分页查询:

SELECT * FROM table_name LIMIT num OFFSET offset_num;

7.使用GROUP BY进行分组查询:

SELECT column1, column2, COUNT(*) FROM table_name GROUP BY column1, column2;

8.使用HAVING进行分组后的条件过滤:

SELECT column1, column2, COUNT(*) FROM table_name GROUP BY column1, column2 HAVING condition;

9. 使用JOIN进行多表联接查询:

SELECT column1, column2 FROM table1 JOIN table2 ON table1.column_name = table2.column_name;

10.使用DISTINCT去除重复行:

SELECT DISTINCT column1, column2 FROM table_name;

11.使用IN子句查询多个值:

SELECT * FROM table_name WHERE column_name IN (value1, value2, ...);

12.使用LIKE进行模糊匹配:

SELECT * FROM table_name WHERE column_name LIKE 'value%';

13.使用BETWEEN进行范围查询:

SELECT * FROM table_name WHERE column_name BETWEEN value1 AND value2;

14.使用CASE语句进行条件判断:

SELECT column1, CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE result3 END FROM table_name;

15.使用COUNT函数进行计数查询:

SELECT COUNT(*) FROM table_name;

16.使用SUM、AVG、MIN、MAX等聚合函数进行统计查询:

  1. SELECT SUM(column_name) FROM table_name;
  2. SELECT AVG(column_name) FROM table_name;
  3. SELECT MIN(column_name) FROM table_name;
  4. SELECT MAX(column_name) FROM table_name;

17.使用GROUP_CONCAT函数进行字符串拼接查询:

SELECT GROUP_CONCAT(column_name) FROM table_name;

18.使用DISTINCT和GROUP_CONCAT联合进行去重字符串拼接:

SELECT GROUP_CONCAT(DISTINCT column_name) FROM table_name;

19.使用AS关键字给查询结果的列起别名:

SELECT column_name AS alias_name FROM table_name;

20.使用IFNULL函数处理NULL值:

SELECT IFNULL(column_name, default_value) FROM table_name;

21.使用CASE语句进行多条件判断:

SELECT column1, CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE result3 END AS alias_name FROM table_name;

22.使用JOIN进行多表联接查询,并使用别名简化表名:

SELECT t1.column1, t2.column2 FROM table1 AS t1 JOIN table2 AS t2 ON t1.column_name = t2.column_name;

23.使用UNION合并查询结果:

  1. SELECT column1, column2 FROM table1
  2. UNION
  3. SELECT column1, column2 FROM table2;

24.使用UNION ALL合并查询结果并保留重复行:

  1. SELECT column1, column2 FROM table1
  2. UNION ALL
  3. SELECT column1, column2 FROM table2;

25.使用EXISTS进行子查询判断:

SELECT column1, column2 FROM table_name WHERE EXISTS (SELECT * FROM other_table WHERE condition);

26.使用NOT EXISTS进行子查询否定判断:

SELECT column1, column2 FROM table_name WHERE NOT EXISTS (SELECT * FROM other_table WHERE condition);

27.使用ANY或SOME进行子查询条件比较:

SELECT column1, column2 FROM table_name WHERE column_name operator ANY (SELECT column_name FROM other_table WHERE condition);

28.使用ALL进行子查询条件比较:

SELECT column1, column2 FROM table_name WHERE column_name operator ALL (SELECT column_name FROM other_table WHERE condition);

29.使用JOIN和子查询进行复杂联接:

SELECT t1.column1, t2.column2 FROM table1 AS t1 JOIN (SELECT * FROM table2 WHERE condition) AS t2 ON t1.column_name = t2.column_name;

30.使用自连接进行表的自联接查询:

SELECT t1.column1, t2.column2 FROM table_name AS t1 JOIN table_name AS t2 ON t1.column_name = t2.column_name;

以上内容仅为个人学习笔记,不对的地方可以指出。
不同的查询语法适用于不同的场景,可以根据具体的业务需求和数据结构选择合适的查询语法

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/381305?site
推荐阅读
相关标签
  

闽ICP备14008679号