mysql 数据库 IV(多表查询)

℡╲_俬逩灬. 提交于 2020-01-14 19:04:16

1.今日内容

  1. 多表联合查询

    • 内连接 (inner jion ...on ...) :只连接匹配的行
    • 外连接:只连接匹配的行
      • 左外连接 left jion ... on ...
      • 右外连接 right jion ... on ...
      • 全外连接 full jion
    select 字段列表 from 表1 inner|left|right join 
                                        表2 on 表1.字段 = 表2.字段
  2. 子查询

2.具体内容

  1. 数据准备


    数据准备示例

    mysql> use day41;
    Database changed
    
    
    # 建表
    mysql> create table department(
        -> id int,
        -> name varchar(20) 
        -> );
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> create table employee(
        -> id int primary key auto_increment,
        -> name varchar(20),
        -> sex enum('male','female') not null default 'male',
        -> age int,
        -> dep_id int
        -> );
    Query OK, 0 rows affected (0.02 sec)
    
    # 插入数据
    mysql> insert into department values
        -> (200,'技术'),
        -> (201,'人力资源'),
        -> (202,'销售'),
        -> (203,'运营');
    Query OK, 4 rows affected (0.01 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    
    mysql> insert into employee(name,sex,age,dep_id) values
        -> ('egon','male',18,200),
        -> ('alex','female',48,201),
        -> ('wupeiqi','male',38,201),
        -> ('yuanhao','female',28,202),
        -> ('liwenzhou','male',18,200),
        -> ('jingliyang','female',18,204)
        -> ;
    Query OK, 6 rows affected (0.00 sec)
    Records: 6  Duplicates: 0  Warnings: 0
    
    # 查看表结构和数据
    mysql> desc department;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(11)     | YES  |     | NULL    |       |
    | name  | varchar(20) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.01 sec)
    
    mysql> desc employee;
    +--------+-----------------------+------+-----+---------+----------------+
    | Field  | Type                  | Null | Key | Default | Extra          |
    +--------+-----------------------+------+-----+---------+----------------+
    | id     | int(11)               | NO   | PRI | NULL    | auto_increment |
    | name   | varchar(20)           | YES  |     | NULL    |                |
    | sex    | enum('male','female') | NO   |     | male    |                |
    | age    | int(11)               | YES  |     | NULL    |                |
    | dep_id | int(11)               | YES  |     | NULL    |                |
    +--------+-----------------------+------+-----+---------+----------------+
    5 rows in set (0.00 sec)
    
    mysql> select * from department;
    +------+--------------+
    | id   | name         |
    +------+--------------+
    |  200 | 技术         |
    |  201 | 人力资源     |
    |  202 | 销售         |
    |  203 | 运营         |
    +------+--------------+
    4 rows in set (0.00 sec)
    
    mysql> select * from employee;
    +----+------------+--------+------+--------+
    | id | name       | sex    | age  | dep_id |
    +----+------------+--------+------+--------+
    |  1 | egon       | male   |   18 |    200 |
    |  2 | alex       | female |   48 |    201 |
    |  3 | wupeiqi    | male   |   38 |    201 |
    |  4 | yuanhao    | female |   28 |    202 |
    |  5 | liwenzhou  | male   |   18 |    200 |
    |  6 | jingliyang | female |   18 |    204 |
    +----+------------+--------+------+--------+
    6 rows in set (0.00 sec)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!