多表连接查询:
以一个共同的字段,求两张表当中符合条件的并集。 通过共同字段把这两张表连接起来。
常用的连接:
内连接:根据表中的共同字段进行匹配
外连接:左外连接、右外链接。
内连接
内连接:根据表中的共同字段进行匹配,将两个表中符合条件的记录进行拼接。
语法:
select 字段 from 表1 inner join 表2 on 表1.字段=表2.字段
例1:查询每个学生的学号、姓名以及各科成绩
stu_info表的表结构
stu_chj表的表结构
创建stu_info和stu_chj表
mysql> create table stu_info(xh int primary key,xm varchar(30) not null,sex enum(‘M’,‘W’)) engine=innodb default charset=utf8;
mysql> create table stu_chj(xh int,yw float(5,2),shx float(5,2));
向stu_info和stu_chj表插入测试的数据
mysql>insert into stu_info values(1,‘lisi’,‘M’),(2,‘lier’,‘W’),(3,‘zhs’,‘M’),(4,‘liming’,‘W’);
mysql> insert into