博客
关于我
python3 mysql 封装多表_python3 mysql-多表查询
阅读量:798 次
发布时间:2023-03-08

本文共 2533 字,大约阅读时间需要 8 分钟。

准备工作

首先,我们需要准备两张表,分别是部门表和员工表。部门表的结构如下:

create table department(    id int,    name varchar(20));

而员工表的结构则稍微复杂一些:

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

接下来,我们需要插入一些数据。部门表的数据如下:

insert into department values (200,'技术'), (201,'人力资源'), (202,'销售'), (203,'运营');

员工表的数据插入如下:

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), ('nvshen','male',18,200), ('xiaomage','female',18,204);

为了确保数据正确,我们可以通过以下命令查看表的结构和数据:

mysql> desc department;mysql> desc employee;

多表连接查询

在实际应用中,我们可能需要查询员工的信息以及他们所属的部门。为了实现这一点,我们需要对两张表进行连接查询。常见的连接方式包括内连接、左连接、右连接和全外连接。

1.笛卡尔积(不推荐使用)

笛卡尔积会生成两张表的所有组合结果,但这种方式没有匹配条件,通常用于理解连接的概念:

mysql> select * from employee, department;

2.内连接

内连接是默认连接方式,只会显示两张表有匹配记录的行:

mysql> select employee.id, employee.name, employee.age, department.name     from employee inner join department     on employee.dep_id = department.id;

3.左连接

左连接优先显示左表的所有记录,即便右表没有对应的记录也会显示:

mysql> select employee.id, employee.name, department.name     from employee left join department     on employee.dep_id = department.id;

4.右连接

右连接优先显示右表的所有记录:

mysql> select employee.id, employee.name, department.name     from employee right join department     on employee.dep_id = department.id;

5.全外连接(注意:MySQL不支持全外连接,但可以通过组合左连接和右连接实现)

全外连接会显示两张表没有匹配记录的部分:

mysql> select * from employee     left join department     on employee.dep_id = department.id    union all select * from employee     right join department     on employee.dep_id = department.id;

符合条件连接查询

有时我们需要根据特定条件查询数据。以下是两个示例:

示例1:查询年龄大于25岁的员工信息

select employee.name, department.name from employee inner join department on employee.dep_id = department.id where age > 25;

示例2:按年龄排序查询

select employee.id, employee.name, employee.age, department.name from employee, department where employee.dep_id = department.id and age > 25order by age asc;

子查询

子查询是一种将查询嵌套在另一个查询中的技术,常用于筛选或计算。

示例1:通过子查询筛选部门

select id, name from department where id in (select dep_id              from employee              group by dep_id              having avg(age) > 25);

示例2:查询年龄高于平均年龄的员工

select t1.name, t1.age from employee t1 inner join (select dep_id, avg(age) as avg_age            from employee            group by dep_id) t2 on t1.dep_id = t2.dep_id where t1.age > t2.avg_age;

示例3:使用EXISTS关键字

select * from employee where exists (select id from department where id = 200);

通过以上方法,我们可以有效地完成多表查询和数据筛选任务。

转载地址:http://culfk.baihongyu.com/

你可能感兴趣的文章
Python:从 FTP 服务器下载文件
查看>>
python:从列表中创建元组列表
查看>>
Python:从嵌套列表中删除重复项
查看>>
Python:从间隔映射到值
查看>>
Python:使用 shell=False 的子进程调用不起作用
查看>>
Python:修改 PATH 环境变量的平台无关方式
查看>>
Python:假与 0
查看>>
Python:函数使用
查看>>
Python:函数进阶
查看>>
Python:在整数的二进制表示中查找最长的二进制间隙
查看>>
Python:如何从二进制转换为 base 64 并返回?
查看>>
Python:如何以非阻塞方式读取子进程的标准输出
查看>>
Python:如何使用 plotly 制作阴影区域或交替背景颜色?
查看>>
Python:如何根据日期时间获取值的计数
查看>>
python:如何检测串行 COM 上的设备名称/ID
查看>>
Python:如何模拟 datetime.utcnow()?
查看>>
Python:如果DataFrames之间的其他值匹配,则对DataFrame中的值求和
查看>>
Python:字典思维
查看>>
Python:将 HEX 字符串转换为字节
查看>>
Python:将列表与范围列表合并
查看>>