MySQL basic knowledge learning - multi-table query (inner connection, outer connection, self-connection), subquery (4)

multi-table relationship

  • One-to-many
    For example: the relationship between departments and employees (establish a foreign key on the many side)
  • Many-to-many
    Columns such as: the relationship between students and courses (establish two foreign keys in the middle table, one associated course and one management student)
  • One-to-one
    For example: user and user details, mostly used for splitting a single table

multi-table query

inner join outer join self join

The inner join query is where two tables intersect;
implicit inner join syntax: select field list from table 1, table 2 where condition...;
explicit inner join syntax: select field list from table 1 JOIN table 2 on connection condition... ;
Left outer join syntax select field list from table 1 left join table 2 on condition;
query all data in table 1 including some data from the intersection of table 1 and table 2
Right outer join syntax select field list from table 1 right join table 2 on condition ;
All data in query table 2 includes some data from the intersection of table 1 and table 2.
Self-join join syntax select field list from table 1 alias A join table 2 alias B on condition;
self-join query can be an inner join query or an outer join query connection query

create table emp(
    columnId int primary key auto_increment comment '主键',
    username varchar(10) unique not null comment '用户名',
    age int check ( age < 120 && age > 18) comment '年龄',
    dp_id int default '1' comment '部门id',
    vid int default '1' comment '员工id',
    managerId int default '1' comment '经理id',
    grender varchar(1) comment '性别'
) comment '员工表';

insert into emp (username, age, dp_id,vid, managerId, grender) values ('金庸', 40, '2', 1, null, '男'),
                                                        ('张无忌', 30, '1', 2, 1,  '男'),
                                                        ('赵敏', 22, '1', 3, 1,  '男'),
                                                        ('韦一笑', 42, '1',  4, 3, '男'),
                                                        ('周芷若', 30, '3',  5, 3, '男'),
                                                        ('张三丰', 32, '3', 6, 5,  '男'),
                                                        ('李莫愁', 33, '3', 7, 6,  '男');

create table dpTable (
    dp_id int primary key auto_increment,
    name varchar(10) not null unique
) comment '部门表';

insert into dpTable (name) values ('研发部'),('董事长'),('市场'),('后勤'),('总经办');


# 内连接
select * from emp, dpTable where emp.dp_id = dpTable.dp_id;
# 内连接带别称
select e.username,d.name from  emp as e, dpTable as d where e.dp_id = d.dp_id;
# 内连接显示链接  select -- frome table1 join table2 on 条件
select * from emp e join dptable d on e.dp_id = d.dp_id;

# 外连接  左连接会查询两个集合重复的部分和整个坐标的库。 同样右链接也是 select -- frome table1 right join table2 on 条件
select * from emp e LEFT JOIN dptable d on e.dp_id = d.dp_id;
select d.name, e.username from emp e right JOIN dptable d on e.dp_id = d.dp_id;

# 自链接  就是一张表当成两张表来查
select a.username as '员工', b.username as '领导' from emp a, emp b where a.managerId = b.vid;

subquery

definition

The nested SELECT statement in the SQL statement is called a nested query or subquery; the inner nested condition is executed first and then the outer condition is executed.
The outer statement of the subquery can be insert, update, delete, select

Multi-table query - subquery

According to the subquery results, it is divided into:
Scalar subquery - the result is a single value
Column subquery - the result is a column
Row subquery - the result is a row
Table subquery - the result is multiple rows and multiple columns

select * from emp.e,dpTable d where e.dp_id=d.dp_id and d.name='研发部';

subquery (match a value)

= matches a value

select * from emp where dp_id=(select dp_id  from  dpTable d where name='研发部' );

subquery (match multiple values)

in matches multiple values

select * from emp where dp_id in(select dp_id  from  dpTable d where name='研发部'or name='董事' );

Subquery instance (deduplication, no deduplication, scalar subquery, column subquery, row subquery)

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);


drop table dptable;
drop table emp;

# union all 是不去重的。 union 去重
select * from emp where salary < 5000
union
select * from emp where age > 50;

select * from emp where salary < 5000
union all
select * from emp where age > 50;

# 子链接
# 查询所有销售部门的员工信息
select * from emp where emp.dept_id = (select id from dept where dept.name = '销售部');
-- 2. 查询在 "方东白" 入职之后的员工信息
select * from emp where emp.name = '方东白';
select * from emp where emp.entrydate > (select entrydate from emp where emp.name = '方东白');

-- 列子查询
-- 1. 查询 "销售部""市场部" 的所有员工信息
select e.* from emp as e where e.dept_id in (select d.id from  dept as d where d.name = '市场部' or  d.name = '销售部');

-- 2. 查询比 财务部 所有人工资都高的员工信息
select * from emp where salary > all ( select salary from emp where dept_id = (select id from dept where name = '财务部') );

-- 3. 查询比研发部其中任意一人工资高的员工信息
select * from emp where salary > some ( select salary from emp where dept_id = (select id from dept where name = '研发部') );

-- 行子查询
-- 1. 查询与 "张无忌" 的薪资及直属领导相同的员工信息 ;
select * from emp where (emp.salary, emp.managerid) = (select salary, managerid from emp where emp.name = '张无忌');

-- 表子查询
-- 1. 查询与 "鹿杖客" , "宋远桥" 的职位和薪资相同的员工信息
select * from emp where (job,salary) in ( select job, salary from emp where name = '鹿杖客' or name = '宋远桥' );

-- 2. 查询入职日期是 "2006-01-01" 之后的员工信息 , 及其部门信息
select e.*, d.* from (select * from emp where entrydate > '2006-01-01') e left join dept d on e.dept_id = d.id ;

Guess you like

Origin blog.csdn.net/weixin_45496521/article/details/128984555