msyql三表命令
一、建表
create table student(
stu_no int,
stu_name varchar(10),
sex char(1),
age int(3),
edit varchar(20) )
DEFAULT charset=utf8;
insert into student values
(1,'wang','男',21,'hello'),
(2,'小明','女',22,'haha2'),
(3,'hu','女',23,'haha3'),
(4,'li','男',25,'haha4');
create table course(
c_no int,
c_name varchar(10))DEFAULT charset=utf8;
insert into course values
(1,'计算机原理'),
(2,'java'),
(3,'c'),
(4,'php'),
(5,'py');
create table sc(
sc_no int,
stu_no int,
c_no int,
score int(3))
DEFAULT charset=utf8;
insert into sc values
(1,1,1,80),
(2,2,2,90),
(3,2,1,85),
(4,2,3,70),
(5,2,4,95),
(6,2,5,89);
二、查看表
表结构:
Student学生表(学号、姓名、性别、年龄、编辑)
Course课程表(课程编号、课程名称)
sc选课表(选课编号、学号、课程编号、成绩)



都和sc表有关系
三、三表连接
1、三表隐藏内连接;
格式:select * from 表1,表2,表3 where 表1.关联字段1=表3.关联字段3 AND 表2.关联字段2=表3.关联字段 3;
案例:select * from student as a ,course as b,sc as c where a.stu_no=c.stu_no AND b.c_no=c.c_no ;

** 2、三表内连接**
内连接 格式:select * from 表1 INNER JOIN 表3 on 表1.关联字段1=表3.关联字段3 INNER JOIN表2 on 表2.关联字段2=表3.关联字段3
案例:select * from student as a INNER JOIN sc as c on a.stu_no=c.stu_no INNER JOIN course as b on b.c_no=c.c_no

** 3、三表左连接**
格式:select * from 表1 left JOIN 表3 on 表1.关联字段1=表3.关联字段3 left JOIN表2 on 表2.关联字段2=表3.关联字段3
案例:select * from student as a left JOIN sc as c on a.stu_no=c.stu_no left JOIN course as b on b.c_no=c.c_no

4、三表右连接
格式:格式:select * from 表1 right JOIN 表3 on 表1.关联字段1=表3.关联字段3 right JOIN表2 on 表2.关联字段2=表3.关联字段3
案例:select * from student as a right JOIN sc as c on a.stu_no=c.stu_no right JOIN course as b on b.c_no=c.c_no

5、左独有数据
格式:
select * from 表1 left JOIN 表2 on 表1.关联字段=表2.关联字 where 右表字段 is null
案例:
select * from dept left JOIN emp on dept.dept1=emp.dept2 where name is null
截图

6、右独有数据
格式:
select * from 表1 right JOIN 表2 on 表1.关联字段=表2.关联字 where 左表字段 is null
案例:
select * from dept right JOIN emp on dept.dept1=emp.dept2 where dept_name is null

7、全外连接:
(1)内连接+左独有数据+右独有数据
案例:
select * from dept INNER JOIN emp on dept.dept1=emp.dept2
UNION
select * from dept left JOIN emp on dept.dept1=emp.dept2
UNION
select * from dept right JOIN emp on dept.dept1=emp.dept2;

(2)左连接+右独有数据
案例:
select * from dept left JOIN emp on dept.dept1=emp.dept2
union
select * from dept right JOIN emp on dept.dept1=emp.dept2 where dept_name is null ;

(3)右连接+左独有数据
案例:
select * from dept right JOIN emp on dept.dept1=emp.dept2
union
select * from dept left JOIN emp on dept.dept1=emp.dept2 where name is null
8、去除重复字段
在显示字段中直接更改
select * from (select a.* ,b.*,c.sc_no,c.score= from student AS a right JOIN sc c on a.stu_no=c.stu_no right JOIN course as b on b.c_no=c.c_no)s where s.c_name="计算机原理"

四、总结:
==多表:
普通内连接:select * from 表1 inner join 表2 on 表 1.关联字段=表2.关联字段
隐藏内连接:select * from 表1,表2 where 表 1.关联字段=表2.关联字段
左连接:select * from 表1 left join 表2 on 表 1.关联字段=表2.关联字段
右连接 :select * from 表1 right join 表2 on 表 1.关联字段=表2.关联字段
左独有数据:select * from 表1 left join 表2 on 表 1.关联字段=表2.关联字段 where 表2中的字段 is null
右独有数据:select * from 表1 right join 表2 on 表 1.关联字段=表2.关联字段 where 表1 中的字段 isnull
全外连接:union
(1)左独有+右独有+内连接
(2)左连接+右独有
(3)右连接+左独有==
四、作业
1、It技术部入职员工的员工号

2、财务部门的收入总和;
select sum(incoming) FROM dept a INNER JOIN emp b on a.dept1=b.dept2 where dept_name="财务"
浙公网安备 33010602011771号