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 ...”进行显式内连接。