Prince_ZaZa
Go big or go home

查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)

select s.s_id,s.s_name,ifnull(round(avg_score,2),0) as avg_score
from student s
#left join,因为student中为空的也需要展示
left join (
select s_id,avg(s_score) as avg_score 
from score
group by s_id
)t1
on s.s_id=t1.s_id

where avg_score is null or avg_score<60
			

right join:
right join会检查右边表的数据是否都包含在新生成的表中,若是:则和join没有区别;若不是:则用NULL和不包含的行组成新的行加入到新表中。

如果理解了 left join的话,就会发现其实: A right join B = B left join A
A right join B: 是以B为基准;B left join A: 也是以B为基准的

总结:
join: 将两个表按照条件连接起来;
left join: 以左边表为基准,看左边表的数据是否都包含在新生成的表中,若是:则和join一样;若不是:则用NULL和不包含的行组成新的行加入到新表中;
right join: 以右边表为基准,看右边表的数据是否都包含在新生成的表中,若是:则和join一样;若不是:则用NULL和不包含的行组成新的行加入到新表中;

posted on 2022-05-09 12:09  Prince_ZaZa  阅读(208)  评论(0)    收藏  举报