当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回
1 内连接查询
查询的结果为两个表匹配到的数据(表A有, 且表B也有的才显示)
select from 表A inner join 表B;
`select from people inner join class;<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1114755/1624247449085-5952664e-4d96-4657-8f47-13e5d8d1ec17.png#crop=0&crop=0&crop=1&crop=1&height=391&id=GexFR&margin=%5Bobject%20Object%5D&name=image.png&originHeight=391&originWidth=851&originalType=binary&ratio=1&rotation=0&showTitle=false&size=85524&status=done&style=none&title=&width=851)<br />
select * from people inner join class on people.cls_id=class.id;<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1114755/1624247467354-0d78b679-c731-4ade-9810-7e3ec00e4748.png#crop=0&crop=0&crop=1&crop=1&height=393&id=FGfaT&margin=%5Bobject%20Object%5D&name=image.png&originHeight=393&originWidth=859&originalType=binary&ratio=1&rotation=0&showTitle=false&size=82426&status=done&style=none&title=&width=859)
select p.name, c.name from people p inner join class c on p.cls_id=c.id;`
2 外连接查询
(1) 左连接查询
查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充select * from people left join class on people.cls_id=class.id;
(2) 右连接查询
查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充select * from people right join class on people.cls_id=class.id;