14 python与mysql 关联查询

本文介绍了如何使用Python进行MySQL数据库的关联查询,包括查询男生的总分、科目的平均分以及未删除科目的最高分和平均分。通过内连接(INNER JOIN)和GROUP BY语句实现不同查询需求。
摘要由CSDN通过智能技术生成

关联查询:关联两个表之间的查询

  1. 确定实体间是否有关系
  2. 确定是几对几的关系
  3. 确定在哪个实体中建立字段

例子:

  1. 查询男生的姓名、总分
    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

  2. 查询科目的名称、平均分
    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

  3. 查询未删除科目的名称、最高分、平均分
    where subjects.isdelete=0
    subjects.title
    max(scores.score)
    avg(scores.score)
    连接:su

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值