博客
关于我
python3 mysql 封装多表_python3 mysql-多表查询
阅读量:800 次
发布时间: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+Twisted+sqlanydb=ABORT()
查看>>
Python函数之返回值、作用域和局部变量
查看>>
Python+统计学 | 探索常用的数据分析统计分布
查看>>
python, selenium中用于切换帧的函数
查看>>
python函数
查看>>
python----线程、进程、协程的区别及多线程详解
查看>>
python---5
查看>>
python---使用celery分布式系统异步发送短信(云通讯)来验证登录
查看>>
python--003--百分号字符串拼接、format
查看>>
Python--10、线程
查看>>
Python--Redis实战:第四章:数据安全与性能保障:第4节:复制
查看>>
python--基本代码规范
查看>>
Python--抽象类接口类
查看>>
PYTHON--欧拉习题二
查看>>
python--高级应用总结
查看>>
python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
查看>>
Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
查看>>
Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
查看>>
Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
查看>>
python出现 unknown encoding: idna 的处理方法
查看>>