数据库操作(多表操作)

在日常工作中数据库操作使用的比较多的是多表连接操作。

多表操作中分“主表”和“从表”,主表设有主键,从表设有外键,而多表操作就是主表的主键和对应从表的外键要相等,这样才能多表操作。

关键字:

inner join :内连接

left join :左外连接

right join: 右外连接

#用内连接查询学生的姓名和成绩(只查询有成绩的)
select x.name,s.grade from xinxi x inner join score s on x.id=s.xid;
select x.name,s.grade from score s inner join xinxi x on x.id=s.xid;

#用左外连接查询学生的姓名和成绩(如果主表在前查询结果有空值)
select x.name,s.grade from xinxi x left join score s on x.id=s.xid;
select x.name,s.grade from score s left join xinxi x on x.id=s.xid;

#用右外连接查询学生的姓名和成绩(如果主表在前查询结果有空值)
select x.name,s.grade from xinxi x right join score s on x.id=s.xid;`course`
select x.name,s.grade from score s right join xinxi x on x.id=s.xid;

#用笛卡尔积查询学生的姓名和成绩(只查询有成绩的)
select x.name,s.grade from xinxi x,score s where x.id=s.xid;

#三表连接查询学生的姓名、课程名和成绩
select x.name,c.name,s.grade from xinxi x 
inner join score s on x.id=s.xid
inner join course c on c.id=s.cid;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会Hello World

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值