‘‘21天好习惯‘‘第一期-2

#连接查询

是关系数据库中最主要的查询;

1.包括等值与非等值连接查询

2.自身连接

3.外连接

##具体代码

1.查询第2个字为勇的学生姓名和学号及选修的课程号和课程名

select sname,a.sno,b.cno,cname
from student a,sc b,course c
where a.sno=b.sno and b.cno=c.cno and sname like '_勇%'

##运行结果

 ##外连接

select sname,cno,grade
from student a left join sc b on a.sno=b.sno --左外连接

select sname,cno,grade
from student a right join sc b on a.sno=b.sno  --右外连接

select sname,cno,grade
from student a full join sc b on a.sno=b.sno  --全外连接

##扩展

(开放题)该公司现在想做促销活动,想将“数据库系统概论”和其他的图书进行套餐搭配,请你帮忙设计一种方案,并说明理由。
--查找和“数据库系统概论”一同被购买的图书

select  *
from sale_item a ,books b
where b.book_name='数据库系统概论' and a.book_no=b.book_no

select  *
from sale_item c ,books d
where d.book_name<>'数据库系统概论' and c.book_no=d.book_no

select  distinct d.book_no,d.book_name
from sale_item a ,books b,sale_item c ,books d
where b.book_name='数据库系统概论' and a.book_no=b.book_no 
and  d.book_name<>'数据库系统概论' and c.book_no=d.book_no  
and a.order_no=c.order_no

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值