热线电话:13121318867

登录
2019-03-14 阅读量: 783
两表联合查询语句

往student表中添加列cid,大于50岁的cid为1,小于50岁的为2

alter table student
add cid int
update student set cid=case when age>50 then 1
when age<=50 then 2
end

添加日期,用alter关键字

alter table student
add dayy char(14)
update student set dayy='2008-09-10'

在helloworld数据库新建一个班级表,class表

use helloworld
drop table class
create table class(
cname nvarchar(10),
cid bigint,
foreign key (cid) references student (id) --外键,class表中的cid只能取student中id的值
)

一次添加多个数据

select * from class
insert into class(cname, cid)
values('青龙','1'),('白虎','2'),('朱雀','3')

查询学生姓名以及所在班级名称,内连接查询

select st.name,cl.cname from student as st
inner join class as cl on st.cid=cl.cid --只出现都有的,哪个表写前都无所谓

左连接,左边的班级表特有的数据会出现

select * from class as cl
left join student as st on st.cid=cl.cid

0.9178
1
关注作者
收藏
评论(0)

发表评论

暂无数据