一、 多表连接查询

外链接语法
select 字段列表
    from  表1 (inner\left\right) join 表2
    on 表1.字段 = 表2.字段;

 

多表查询,初识pymysql模块
 1 #建表
 2 #部门表
 3 create table department(
 4 id int,
 5 name varchar(20) 
 6 );
 7 
 8 create table employee(
 9 id int primary key auto_increment,
10 name varchar(20),
11 sex enum('male','female') not null default 'male',
12 age int,
13 dep_id int
14 );
15 
16 #给两个表插入一些数据
17 insert into department values
18 (200,'技术'),
19 (201,'人力资源'),
20 (202,'销售'),
21 (203,'运营'); #注意这一条数据,在下面的员工表里面没有对应这个部门的数据
22 
23 insert into employee(name,sex,age,dep_id) values
24 ('egon','male',18,200),
25 ('alex','female',48,201),
26 ('wupeiqi','male',38,201),
27 ('yuanhao','female',28,202),
28 ('liwenzhou','male',18,200),
29 ('jingliyang','female',18,204) #注意这条数据的dep_id字段的值,这个204,在上面的部门表里面也没有对应的部门id。所以两者都含有一条双方没有涉及到的数据,这都是为了演示一下效果设计的昂
30 ;
练习资料

相关文章: