union合并表
union合并两个表的学生信息,消除重复项
select *from student
union select *from student02
union all 合并两个表的学生信息,不消除重复项
select *from student
union all select *from student02
将两张表合并的结果集插入在一张新表中
select name,age,sex,address into student_union
from student union all select name,age,sex,address from student02
inner join内连接的使用
创建学生成绩表
create table grade
(
ksid int not null,
csid int not null,
fs int
)
go
向学生成绩表插入信息(学生ID,测试ID,分数)
insert into grade(ksid,csid,fs)values
('7','71','70'),
('8','81','80'),
('9','91','90'),
('13','913','101')
go
查询学生成绩表
select * from grade
两表(学生信息表和学生成绩表)内连接,查询学生信息及成绩
select st.name,st.age,st.sex,st.address,gr.fs
from student st
inner join grade gr
on st.id = gr.ksid
创建学生课程表
create table course
(
kcid int not null,
kcmc char(8)
)
go
向课程表插入信息(课程ID,课程名字)
insert into course(kcid,kcmc) values
('7','语文'),
('8','计算机'),
('9','数学'),
('13','外语')
go
查询学生课程表
select *from course
内连接查询三张表(学生信息表,成绩表,和课程表)
select st.name,gr.fs,co.kcmc
from student st
inner join grade gr on st.id = gr.ksid
inner join course co on gr.ksid = co.kcid