union合并表与内连接

来源:互联网 发布:有淘宝卖家手机版本 编辑:程序博客网 时间:2024/06/08 17:24

union合并表

union合并两个表的学生信息,消除重复项

select *from studentunion select *from student02

union all 合并两个表的学生信息,不消除重复项

select *from studentunion all select *from student02

将两张表合并的结果集插入在一张新表中

select name,age,sex,address into student_unionfrom 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.fsfrom student stinner join grade gron 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.kcmcfrom student stinner join grade gr on st.id = gr.ksidinner join course co on gr.ksid = co.kcid
0 0
原创粉丝点击