首页 > 解决方案 > 我有 2 个关于使用 2 个表的 sqlite3 查询的问题

问题描述

问题基于此图像

A) 按讲师姓名的顺序列出每位讲师及其教授的每个模块以及学习该模块的学生人数。

B) 输出每个人通过模块的模块数量(假设通过分数为 40)。

标签: sqlite

解决方案


对于 A,您需要连接 2 个表,然后group by lecturer, module计算每个组的行数(每行对应一个学生):

select t.lecturer, t.module, count(*) numberofstudents
from teaches t inner join studies s
on s.module = t.module
group by t.lecturer, t.module
order by t.lecturer

对于 B,使用NOT EXISTS查找所有成绩所在的模块>= 40并计算它们:

select count(distinct module) numberofmodules
from studies s
where not exists (
  select 1 from studies 
  where module = s.module and grade < 40
)

推荐阅读