mysql练习——“查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩”不同SQL语句;了解显式内连接和隐式内连接

1.数据准备

1.1 数据表名:student

1.2 数据表名:stu_sco

2.写法1,使用关键字“join ... on ...”进行表关联,查询符合条件的数据(显式内连接,在“join ... on ...”连接时,使用and直接过滤符合条件的数据,提高一定的性能)

select stu_sco.s_id,student.s_name,avg(stu_sco.score)
from student join stu_sco on student.s_id = stu_sco.s_id
and stu_sco.score < 60
group by stu_sco.s_id,student.s_name
having count(*) >= 2;

3.写法2,使用关键字“join ... on ...”进行表关联,查询符合条件的数据(显式内连接,使用where过滤符合条件的数据;)

select stu_sco.s_id,student.s_name,avg(stu_sco.score)
from student join stu_sco on student.s_id = stu_sco.s_id
where stu_sco.score < 60
group by stu_sco.s_id,student.s_name
having count(*) >= 2;

4.写法3,使用关键字“where”进行关联,查询符合条件的数据(隐式内连接,可读性较差,性能较低;原因:在连接后对结果集进行全局过滤(如统计、聚合后筛选))

select stu_sco.s_id,student.s_name,avg(stu_sco.score)
from student,stu_sco
where stu_sco.score < 60 and student.s_id = stu_sco.s_id
group by stu_sco.s_id,student.s_name
having count(*) >= 2;

5总结

需要进行表连接查询时,建议使用关键字“join ... on ...”进行显式内连接。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值