当前位置:   article > 正文

写了6年SQL,推荐快速上手MySQL 的SQL语句

写了6年SQL,推荐快速上手MySQL 的SQL语句

写了6、7年SQL,有写复杂,有写简单,但总体而言,基础是统一的,。就像编程之需要会加减乘除,用MySQL懂这些SQL就足够了。


MySQL 查询语句是用于从数据库中检索数据的语句。以下是一些常见的 MySQL 查询语句,并附详细说明和示例:

1. SELECT 查询

image.png

基本查询

SELECT column1, column2, ...
FROM table_name;
  • 1
  • 2

示例:

SELECT first_name, last_name
FROM employees;
  • 1
  • 2

employees 表中选择 first_namelast_name 字段。

image.png

查询所有列

SELECT *
FROM table_name;
  • 1
  • 2

示例:

SELECT *
FROM employees;
  • 1
  • 2

employees 表中选择所有列。

带条件的查询

SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • 1
  • 2
  • 3

示例:

SELECT first_name, last_name
FROM employees
WHERE department = 'Sales';
  • 1
  • 2
  • 3

employees 表中选择 Sales 部门的 first_namelast_name 字段。

2. WHERE 子句

使用 ANDOR 逻辑运算符

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 OR condition3;
  • 1
  • 2
  • 3

示例:

SELECT first_name, last_name
FROM employees
WHERE department = 'Sales' AND salary > 50000;
  • 1
  • 2
  • 3

employees 表中选择 Sales 部门且薪水大于 50000 的员工的 first_namelast_name 字段。

3. ORDER BY 子句

按指定列排序

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 ASC|DESC, column2 ASC|DESC, ...;
  • 1
  • 2
  • 3

示例:

SELECT first_name, last_name
FROM employees
ORDER BY last_name ASC, first_name DESC;
  • 1
  • 2
  • 3

last_name 升序和 first_name 降序排列 employees 表中的数据。

4. GROUP BY 子句

按指定列分组并聚合数据

SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1;
  • 1
  • 2
  • 3

示例:

SELECT department, COUNT(*)
FROM employees
GROUP BY department;
  • 1
  • 2
  • 3

department 列对 employees 表中的数据进行分组,并计算每个部门的员工数量。

5. HAVING 子句

对分组后的结果进行过滤

SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1
HAVING condition;
  • 1
  • 2
  • 3
  • 4

示例:

SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
  • 1
  • 2
  • 3
  • 4

筛选出员工数量大于 5 的部门。
image.png

6. JOIN 查询

内连接

SELECT columns
FROM table1
INNER JOIN

table2
ON table1.column = table2.column;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

示例:

SELECT employees.first_name, employees.last_name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
  • 1
  • 2
  • 3
  • 4

employees 表和 departments 表进行内连接,选择 first_namelast_namedepartment_name

左连接

SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
  • 1
  • 2
  • 3
  • 4

示例:

SELECT employees.first_name, employees.last_name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
  • 1
  • 2
  • 3
  • 4

employees 表中选择所有记录,并匹配 departments 表中的记录,如果没有匹配到则返回 NULL。

右连接

SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
  • 1
  • 2
  • 3
  • 4

示例:

SELECT employees.first_name, employees.last_name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
  • 1
  • 2
  • 3
  • 4

departments 表中选择所有记录,并匹配 employees 表中的记录,如果没有匹配到则返回 NULL。

7. 子查询

在 SELECT 中使用子查询

SELECT column1
FROM table_name
WHERE column2 = (SELECT column
                 FROM another_table
                 WHERE condition);
  • 1
  • 2
  • 3
  • 4
  • 5

示例:

SELECT first_name, last_name
FROM employees
WHERE department_id = (SELECT department_id
                       FROM departments
                       WHERE department_name = 'Sales');
  • 1
  • 2
  • 3
  • 4
  • 5

employees 表中选择 Sales 部门的所有员工。

在 FROM 子句中使用子查询

SELECT subquery.column1, subquery.column2
FROM (SELECT column1, column2
      FROM table_name
      WHERE condition) AS subquery;
  • 1
  • 2
  • 3
  • 4

示例:

SELECT sub.first_name, sub.last_name
FROM (SELECT first_name, last_name
      FROM employees
      WHERE department_id = 1) AS sub;
  • 1
  • 2
  • 3
  • 4

从子查询结果中选择 department_id 为 1 的员工的 first_namelast_name

8. INSERT INTO 查询

插入数据

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
  • 1
  • 2

示例:

INSERT INTO employees (first_name, last_name, department_id, salary)
VALUES ('John', 'Doe', 3, 50000);
  • 1
  • 2

employees 表中插入一条新记录。

9. UPDATE 查询

更新数据

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
  • 1
  • 2
  • 3

示例:

UPDATE employees
SET salary = 55000
WHERE last_name = 'Doe';
  • 1
  • 2
  • 3

更新 last_name 为 ‘Doe’ 的员工的 salary 为 55000。

10. DELETE 查询

删除数据

DELETE FROM table_name
WHERE condition;
  • 1
  • 2

示例:

DELETE FROM employees
WHERE last_name = 'Doe';
  • 1
  • 2

删除 last_name 为 ‘Doe’ 的员工记录。

11. LIMIT 子句

限制结果集

SELECT column1, column2, ...
FROM table_name
LIMIT number;
  • 1
  • 2
  • 3

示例:

SELECT first_name, last_name
FROM employees
LIMIT 10;
  • 1
  • 2
  • 3

选择前 10 条员工记录。

12. DISTINCT 关键字

去重查询

SELECT DISTINCT column1, column2, ...
FROM table_name;
  • 1
  • 2

示例:

SELECT DISTINCT department_id
FROM employees;
  • 1
  • 2

选择 employees 表中所有不同的 department_id

这些示例涵盖了 MySQL 中的基本查询语句及其常见用法。通过这些示例,你可以构建更复杂的查询来满足不同的数据检索需求。

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

闽ICP备14008679号