2018-07-29MySQL的关系连接查询

2018-07-29  本文已影响8人  菩灵

先看个问题

select students.sname,subjects.stitle,scores.score
from scores
inner join students on scores.stuid=students.id
inner join subjects on scores.subid=subjects.id;

连接查询

什么时候需要用到连接查询:需要查看一组信息,来自多张表。

注意:inner join 只有在关系匹配上的时候才会在结果集中出现,inner join不分前后。

inner join匹配上的会出现

练习

select students.sname,avg(scores.score)
from scores
inner join students on scores.stuid=students.id
group by students.sname;

select students.sname,avg(scores.score)
from scores
inner join students on scores.stuid=students.id
where students.gender=1
group by students.sname;

select subjects.stitle,avg(scores.score)
from scores
inner join subjects on scores.subid=subjects.id
group by subjects.stitle;

select subjects.stitle,avg(scores.score),max(scores.score)
from scores
inner join subjects on scores.subid=subjects.id
where subjects.isdelete=0
group by subjects.stitle;
上一篇下一篇

猜你喜欢

热点阅读