关联查询:关联两个表之间的查询
- 确定实体间是否有关系
- 确定是几对几的关系
- 确定在哪个实体中建立字段
例子:
-
查询男生的姓名、总分
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

本文介绍了如何使用Python进行MySQL数据库的关联查询,包括查询男生的总分、科目的平均分以及未删除科目的最高分和平均分。通过内连接(INNER JOIN)和GROUP BY语句实现不同查询需求。
最低0.47元/天 解锁文章
1509

被折叠的 条评论
为什么被折叠?



