博客
关于我
python3 mysql 封装多表_python3 mysql-多表查询
阅读量:797 次
发布时间: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列表 行列选择_python_pandas_dataframe_行列选择_切片操作
查看>>
python列表
查看>>
python列出当前目录、子目录和文件的脚本
查看>>
python+flask计算机毕业设计骨科门诊患者档案管理系统(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校体测管理系统的设计与实现(程序+开题+论文)
查看>>
Python切片对象和__getitem__
查看>>
python+flask计算机毕业设计高校体育赛事系统(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校体育运动会比赛系统(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校危化品管理系统(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校团委事务管理系统(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校学生选课系统(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校学生饮食推荐系统(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校教务管理系统(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校教学档案管理系统(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校教学督导平台(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校数字化迎新系统(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校校内景点平台(程序+开题+论文)
查看>>
python+flask计算机毕业设计高校校园线上跳蚤市场的系统设计(程序+开题+论文)
查看>>
Python图形处理
查看>>
python+flask计算机毕业设计高校物品使用互益平台的设计与实现(程序+开题+论文)
查看>>