MySQL database - multi-table query (2) - inner connection, outer connection

Table of contents 

inner join

Query syntax

Inner join demo

outer join

Query syntax

External connection demonstration


inner join

The inner join queries the intersection of the two tables and returns the data from the intersection of table A and table B. Inner joins are divided into two forms: implicit inner joins and explicit inner joins.

Query syntax

implicit inner join

SELECT 字段列表 FROM 表1,表2 WHERE 条件...;

explicit inner join

SELECT 字段列表 FROM 表1 [INNER] JOIN 表2 ON 连接条件 ...;

Inner join demo

Prepare data

create table dept(
    id   int auto_increment comment 'ID' primary key,
    name varchar(50) not null comment '部门名称'
)comment '部门表';

create table emp(
    id  int auto_increment comment 'ID' primary key,
    name varchar(50) not null comment '姓名',
    age  int comment '年龄',
    job varchar(20) comment '职位',
    salary int comment '薪资',
    entrydate date comment '入职时间',
    managerid int comment '直属领导ID',
    dept_id int comment '部门ID'
)comment '员工表';

-- 添加外键
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);

INSERT INTO dept (id, name)
    VALUES (1, '研发部'), (2, '市场部'),(3, '财务部'), (4, '销售部'), (5, '总经办'), (6, '人事部');
INSERT INTO emp (id, name, age, job,salary, entrydate, managerid, dept_id) VALUES
            (1, '金庸', 66, '总裁',20000, '2000-01-01', null,5),
            (2, '张无忌', 20, '项目经理',12500, '2005-12-05', 1,1),
            (3, '杨逍', 33, '开发', 8400,'2000-11-03', 2,1),
            (4, '韦一笑', 48, '开发',11000, '2002-02-05', 2,1),
            (5, '常遇春', 43, '开发',10500, '2004-09-07', 3,1),
            (6, '小昭', 19, '程序员鼓励师',6600, '2004-10-12', 2,1),
            (7, '灭绝', 60, '财务总监',8500, '2002-09-12', 1,3),
            (8, '周芷若', 19, '会计',48000, '2006-06-02', 7,3),
            (9, '丁敏君', 23, '出纳',5250, '2009-05-13', 7,3),
            (10, '赵敏', 20, '市场部总监',12500, '2004-10-12', 1,2),
            (11, '鹿杖客', 56, '职员',3750, '2006-10-03', 10,2),
            (12, '鹤笔翁', 19, '职员',3750, '2007-05-09', 10,2),
            (13, '方东白', 19, '职员',5500, '2009-02-12', 10,2),
            (14, '张三丰', 88, '销售总监',14000, '2004-10-12', 1,4),
            (15, '俞莲舟', 38, '销售',4600, '2004-10-12', 14,4),
            (16, '宋远桥', 40, '销售',4600, '2004-10-12', 14,4),
            (17, '陈友谅', 42, null,2000, '2011-10-12', 1,null);

 1. Query the name of each employee and the name of the associated department (implicit inner connection implementation)

  • Table structure: emp, dept
  • Connection condition: emp.dept_id = dept.id
select e.name '姓名', d.name '部门' from emp e,dept d where e.dept_id = d.id;

operation result:

(One employee has no department for the time being)

2. Query the name of each employee and the name of the associated department (explicit inner connection method)

  • Table structure: emp, dept
  • Connection condition: emp.dept_id = dept.id
select e.name '姓名',d.name '部门' from emp e inner join dept d where e.dept_id = d.id;
-- inner可以省略

operation result:

outer join

Query syntax

left outer join

SELECT 字段列表 FROM 表1 LEFT [OUTER] JOIN 表2 ON 条件 ...;

Equivalent to querying all the data in Table 1 (left table), including the data at the intersection of Table 1 and Table 2

right outer join

SELECT 字段列表 FROM 表1 RIGHT [OUTER] JOIN 表2 ON 条件...;

Equivalent to querying all the data in Table 2 (right table), including the data at the intersection of Table 1 and Table 2

External connection demonstration

1. Query all data in the emp table and the corresponding department information (left outer join)

  • Table structure: emp, dept
  • Connection condition: emp.dept_id = dept.id
select e.*,d.name from emp e left outer join dept d on e.dept_id = d.id;
-- outer可省略

search result: 

Syntactically, perform a left outer join query on Table 1 (emp) and Table 2 (dept), that is, the data in Table 1 + the data at the intersection of Table 1 and Table 2. Therefore, employee No. 17 can be queried, which belongs to the content of Table 1, but does not belong to the intersection of the two tables.

2. Query all data in the dept table and the corresponding employee information (right outer join)

select d.*,e.* from emp e right outer join dept d on e.dept_id = d.id;

search result:

In the same way, the right outer join here queries the data of Table 2 + all the data at the intersection of Table 1 and Table 2, so the empty Human Resources Department can be queried.

At the same time, you can use left outer join to implement right outer join.

select d.*,e.* from dept d left outer join emp e on e.dept_id = d.id;

You only need to swap the positions of the two tables. The left outer join of table 1 (dept) and table 2 (emp) here is equivalent to the right outer join of table 1 (emp) and table 2 (dept) in the previous place.

Guess you like

Origin blog.csdn.net/li13437542099/article/details/132460975