博客
关于我
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/

你可能感兴趣的文章
Python3-selenium自动化测试框架,结合HTMLTestRunner测试报告!
查看>>
Python3-如何自我更新作为服务运行的应用程序
查看>>
Python3.1-网格模拟概念性问题
查看>>
Python3.12 解决conda环境libstdc++版本过低问题
查看>>
python3.2批量查询IP地址区域
查看>>
Python3.5中协程和未来/任务之间的区别?
查看>>
python3.5实现socket通讯(TCP)
查看>>
python3.6 ubuntu部署nginx、 uwsgi、 django
查看>>
Python3.6.3中,functools似乎不能用
查看>>
python3.6.5+cuda9+cudnn7.1+win10+tensorflow-gpu1.9.0下载配置
查看>>
python3.6使用pickle序列化class
查看>>
python3.6入门到高阶(全栈) day027 网络通信协议 osi七层模型
查看>>
Python3.6属性错误:模块‘;异步没有属性‘;运行‘;
查看>>
python3.6版本中的python-weka-wrapper安装失败,错误代码为%1
查看>>
Python3.7安装cv2踩过的坑
查看>>
python写txt文件
查看>>
python关于sorted排序函数详细解析 附实战代码(全)
查看>>
python3.8调用m文件,调用matlab
查看>>
python3.9新特性_Python 3.9 新特性速览
查看>>
Python3.x:遍历select下拉框获取value值
查看>>