关联查询:关联两个表之间的查询
- 确定实体间是否有关系
- 确定是几对几的关系
- 确定在哪个实体中建立字段
例子:
-
查询男生的姓名、总分
students.gender = 1
students.name
sum(scores.score)
建立连接:students.id=scores.stuid
使用sum->分组,id:每个人的总分命令:
select name.sum(score)
from students
inner join scores on students.id=scores.stuid
where gender=1
group by students.id -
查询科目的名称、平均分
subjects.title
avg(scores.score)
连接:subjects.id=scores.subid
avg->group by subjects.title命令:
select subjects.title, avg(scores.score)
from scores
inner join subjects on scores.subid=subjects.id
group by subjects.title -
查询未删除科目的名称、最高分、平均分
where subjects.isdelete=0
subjects.title
max(scores.score)
avg(scores.score)
连接:su