当前位置:   article > 正文

SQL表连接方式_sql 表连接

sql 表连接

一、SQL中的表连接方式:

  1. 内连接(INNER JOIN):返回两个表中符合连接条件的交集。
  2. 外连接(OUTER JOIN):
    • 左外连接(LEFT JOIN):返回左表中所有记录,以及右表中符合连接条件的记录。
    • 右外连接(RIGHT JOIN):返回右表中所有记录,以及左表中符合连接条件的记录。
    • 完全外连接(FULL JOIN):返回左表和右表中的所有记录,如果没有匹配的记录则用NULL填充。
  3. 交叉连接(CROSS JOIN):返回两个表的笛卡尔积,即左表中的每一行都与右表中的每一行组合。
  4. 自连接(SELF JOIN):将表与自身进行连接,常用于比较表中不同行之间的数据。

这些连接方式允许在不同的条件下将多个表关联起来,以满足查询的需求。

二、创建两个表:

employees(员工信息)和departments(部门信息):
  1. CREATE TABLE departments (
  2. department_id INT PRIMARY KEY,
  3. department_name VARCHAR(50)
  4. );
  5. CREATE TABLE employees (
  6. employee_id INT PRIMARY KEY,
  7. employee_name VARCHAR(50),
  8. department_id INT,
  9. manager_id INT,
  10. FOREIGN KEY (department_id) REFERENCES departments(department_id),
  11. FOREIGN KEY (manager_id) REFERENCES employees(employee_id)
  12. );

departments表有两个列:department_iddepartment_name,其中department_id是主键。

employees表有四个列:employee_idemployee_namedepartment_idmanager_id,其中employee_id是主键,department_id是外键关联到departments表的department_idmanager_id是外键关联到employees表的employee_id,表示员工的直接上级。

这样就创建了两个简单的表,可以用于存储员工和部门的信息。

插入数据:

  1. -- 向 departments 表插入数据
  2. INSERT INTO departments (department_id, department_name) VALUES
  3. (1, 'IT'),
  4. (2, 'HR'),
  5. (3, 'Finance'),
  6. (4, 'Marketing'),
  7. (5, 'Sales');
  8. -- 向 employees 表插入数据
  9. INSERT INTO employees (employee_id, employee_name, department_id, manager_id) VALUES
  10. (1, 'John Doe', 1, NULL),
  11. (2, 'Jane Smith', 2, 1),
  12. (3, 'David Lee', 1, 1),
  13. (4, 'Emily Brown', 3, 1),
  14. (5, 'Michael Johnson', 4, 3),
  15. (6, 'Lisa Wang', 1, 3),
  16. (7, 'Tom Davis', 5, 4),
  17. (8, 'Sarah White', 5, 4),
  18. (9, 'Kevin Martin', 2, 1),
  19. (10, 'Mary Wilson', 3, 1);

三、演示连接方式:

1.内连接(INNER JOIN):

  1. SELECT e.employee_name, e.employee_id, d.department_name
  2. FROM employees e
  3. INNER JOIN departments d ON e.department_id = d.department_id;

结果:

  1. employee_name | employee_id | department_name
  2. -----------------------------------------------
  3. John Doe | 1 | IT
  4. Jane Smith | 2 | HR
  5. David Lee | 3 | IT
  6. Emily Brown | 4 | Finance
  7. Michael Johnson | 5 | Marketing
  8. Lisa Wang | 6 | IT
  9. Tom Davis | 7 | Sales
  10. Sarah White | 8 | Sales
  11. Kevin Martin | 9 | HR
  12. Mary Wilson | 10 | Finance

2.左外连接(LEFT JOIN):

  1. SELECT e.employee_name, e.employee_id, d.department_name
  2. FROM employees e
  3. LEFT JOIN departments d ON e.department_id = d.department_id;

结果:

  1. employee_name | employee_id | department_name
  2. -----------------------------------------------
  3. John Doe | 1 | IT
  4. Jane Smith | 2 | HR
  5. David Lee | 3 | IT
  6. Emily Brown | 4 | Finance
  7. Michael Johnson | 5 | Marketing
  8. Lisa Wang | 6 | IT
  9. Tom Davis | 7 | Sales
  10. Sarah White | 8 | Sales
  11. Kevin Martin | 9 | HR
  12. Mary Wilson | 10 | Finance

3.右外连接(RIGHT JOIN):

  1. SELECT e.employee_name, e.employee_id, d.department_name
  2. FROM employees e
  3. RIGHT JOIN departments d ON e.department_id = d.department_id;

结果:

  1. employee_name | employee_id | department_name
  2. -----------------------------------------------
  3. John Doe | 1 | IT
  4. Jane Smith | 2 | HR
  5. David Lee | 3 | IT
  6. Emily Brown | 4 | Finance
  7. Michael Johnson | 5 | Marketing
  8. Lisa Wang | 6 | IT
  9. Tom Davis | 7 | Sales
  10. Sarah White | 8 | Sales
  11. Kevin Martin | 9 | HR
  12. Mary Wilson | 10 | Finance

4.交叉连接(CROSS JOIN):

  1. SELECT e.employee_name, d.department_name
  2. FROM employees e
  3. CROSS JOIN departments d;

结果会返回所有可能的员工和部门组合,共50条记录(10个员工 * 5个部门)。

5.自连接(SELF JOIN):

假设我们想要查找每个员工的经理,我们可以使用自连接:

  1. SELECT e.employee_name, m.employee_name AS manager_name
  2. FROM employees e
  3. LEFT JOIN employees m ON e.manager_id = m.employee_id;

这将返回每个员工及其对应的经理的姓名。

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

闽ICP备14008679号