zoukankan      html  css  js  c++  java
  • MySQL多表查询和子查询

    前期表准备

        # 建表
            create table dep(
                id int,
                name varchar(20)
            );
            create table emp1(
                id int primary key auto_increment,
                name varchar(20),
                sex enum('male','female') not null default 'male',
                age int,
                dep_id int
            );
        # 插入数据
            insert into dep values(200,'技术'),(201,'人力资源'),(202,'销售'),(203,'运营'),(205,'公关');
            insert into emp1(name,sex,age,dep_id) values
            ('jason','male',18,200),
            ('egon','female',48,201),
            ('kevin','male',18,202),
            ('nick','male',29,202),
            ('owen','male',18,203),
            ('jerry','female',33,204);

    多表查询

    '''
        select * from dep,emp1; # 结果:笛卡尔集
        select * from emp1,dep where emp1.dep_id=dep.id;
            mysql也知道,后面查询过程中,肯定会经常用到拼表操作,所以特地开设了对应的方法
                inner join 内连接
                left join  左连接
                right join 右连接
                union      全连接    
                    select * from emp1 inner join dep on emp1.dep_id = dep.id;
                        # inner join只拼接两张表中公有的数据部分
                    select * from emp1 left join dep on emp1.dep_id = dep.id;
                        # left join"左表所有的数据都展示出来",左表没有对应的项展示null
                    select * from emp1 right join dep on emp1.dep_id = dep.id;
                        # right join"右表所有的数据都展示出来",左表没有对应的项展示null
                    select * from emp1 left join dep on emp1.dep_id = dep.id union select * from emp1 right join dep on emp1.dep_id = dep.id;
                        # union左右两表的数据都展示出来(左右表不对应的项,所对应信息null展示)
    '''

    子查询

    '''
        子查询就是我们平时解决问题的思路
            分步骤解决问题
                第一步
                第二步
                ...
        将一个查询语句的结果当作另外一个查询语句的条件使用
        1.查询部门是技术或者人力资源的员工信息
            A:先获取部门的id号
            B:再去员工表里面筛选出对应的员工
            select * from emp1 where dep_id in (select id from dep where name='技术' or name='人力资源');
        总结:
            表的查询结果可以作为其它表的查询条件,也可以通过起别名的方式把它作为一张虚拟表跟其它表关联
            多表查询就两种方式
                先拼接表再查询
                子查询,一步一步来
    '''

    补充知识点:

    '''
        查询平均年龄再25岁以上的不部门名称(只要是多表查询,就有两种思路,联表和子查询)
            联表操作(涉及到多表操作的时候,一定要加上表的前缀)
                1 先拿到部门和员工表拼接之后的结果
                2 分析语义,得到需要进行分组
                    select dep.name from emp1 inner join dep on emp1.dep_id=dep.id group by dep.name having avg(age)>25;
            子查询
                select name from dep where id in (select dep_id from emp1 group by dep_id having avg(age)>25);
        关键字exists(了解即可)
            只返回布尔值,True/False
                返回True的时候,外层查询语句执行
                返回False的时候,外层查询语句不再执行
                    select * from emp1 where exists (select * from dep where id>100);
                    select * from emp1 where exists (select * from dep where id>300);
    '''
    while True: print('studying...')
  • 相关阅读:
    85. Maximal Rectangle
    120. Triangle
    72. Edit Distance
    39. Combination Sum
    44. Wildcard Matching
    138. Copy List with Random Pointer
    91. Decode Ways
    142. Linked List Cycle II
    异或的性质及应用
    64. Minimum Path Sum
  • 原文地址:https://www.cnblogs.com/xuewei95/p/15116601.html
Copyright © 2011-2022 走看看