下面的查询将如何查询?

问题描述 投票:-1回答:1
SELECT s.subject_id
     , s.subject_name
     , t.class_id
     , t.section_id
     , c.teacher_id
  FROM school_timetable_content c
  JOIN s  
    ON c.subject_id = s.subject_id
  JOIN school_timetables t 
    ON t.timetable_id = c.timetable_id
 WHERE c.teacher_id = 184
   AND t.class_id = 24
   AND t.school_id = 28

从上面的查询中,我得到了如下所示的结果:------。

Result for the above query

再从上面的结果中,我想得到 科目 与所有独特的 第15、16、26节即预期产出 印地语、数学

mysql sql database qsqlquery mysql-select-db
1个回答
1
投票

我们的想法是精确过滤这三个部分。 然后,如果这三个部分都存在,则进行汇总和统计。

SELECT s.subject_id, s.subject_name
FROM school_timetable_content tc JOIN
     school_subjects s
     ON tc.subject_id = s.subject_id JOIN
     school_timetables t
     ON t.timetable_id = tc.timetable_id
WHERE tc.teacher_id = 184 AND 
      t.class_id = 24 AND
      t.school_id = 28 AND
      t.section_id IN (15, 16, 26)
GROUP BY s.subject_id, s.subject_name
HAVING COUNT(*) = 3;

这是假设 section_id 是不重复的。 如果可以,请使用 HAVING(COUNT(DISTINCT section_id)) = 3 来代替。

请注意,使用表的别名可以使查询更容易编写和阅读。

© www.soinside.com 2019 - 2024. All rights reserved.