将子查询转换为联接

发布于 2024-10-17 18:04:48 字数 2078 浏览 0 评论 0原文

我有三个表格,即章节、课程和问题,用于显示课程的问题。下面给出表结构供参考。

表“public.chapters”

  Column   |          Type          |                           Modifiers                           
-----------+------------------------+---------------------------------------------------------------
 id        | integer                | not null default nextval('chapters_chapter_id_seq'::regclass)
 chapter   | character varying(255) | not null
 course_id | integer                | 
 published | boolean                | default false

表“public.lessons”

   Column   |          Type          |                          Modifiers                          
------------+------------------------+-------------------------------------------------------------
 id         | integer                | not null default nextval('lessons_lesson_id_seq'::regclass)
 lesson     | character varying(255) | not null
 chapter_id | integer                | 
 published  | boolean                | default false

表“public.questions”

  Column   |          Type          |                            Modifiers                            
-----------+------------------------+-----------------------------------------------------------------
 id        | integer                | not null default nextval('questions_question_id_seq'::regclass)
 template  | character varying(255) | not null
 wording   | character varying(255) | not null
 lesson_id | integer                | not null

现在,我需要查找一章中的问题数量。所以,我使用以下查询。

select sum( num_of_questions ) as num_of_questions, 
       chapter_id 
 from ( select chapters.id as chapter_id,
               lesson_id, 
               count(*) as num_of_questions 
          from questions 
          JOIN lessons ON lessons.id = questions.lesson_id 
          JOIN chapters ON lessons.chapter_id = chapters.id 
      GROUP BY lesson_id, chapters.id  
      ORDER BY lesson_id, chapters.id) as foo 
group by  chapter_id;

如何,我可以将此查询转换为使用联接而不是子查询吗?

I have three table viz, chapters, lessons, and questions that are used to show questions for a course. The table structure is given below for reference.

Table "public.chapters"

  Column   |          Type          |                           Modifiers                           
-----------+------------------------+---------------------------------------------------------------
 id        | integer                | not null default nextval('chapters_chapter_id_seq'::regclass)
 chapter   | character varying(255) | not null
 course_id | integer                | 
 published | boolean                | default false

Table "public.lessons"

   Column   |          Type          |                          Modifiers                          
------------+------------------------+-------------------------------------------------------------
 id         | integer                | not null default nextval('lessons_lesson_id_seq'::regclass)
 lesson     | character varying(255) | not null
 chapter_id | integer                | 
 published  | boolean                | default false

Table "public.questions"

  Column   |          Type          |                            Modifiers                            
-----------+------------------------+-----------------------------------------------------------------
 id        | integer                | not null default nextval('questions_question_id_seq'::regclass)
 template  | character varying(255) | not null
 wording   | character varying(255) | not null
 lesson_id | integer                | not null

Now, I need to find number of questions in a chapter. So, I use following query.

select sum( num_of_questions ) as num_of_questions, 
       chapter_id 
 from ( select chapters.id as chapter_id,
               lesson_id, 
               count(*) as num_of_questions 
          from questions 
          JOIN lessons ON lessons.id = questions.lesson_id 
          JOIN chapters ON lessons.chapter_id = chapters.id 
      GROUP BY lesson_id, chapters.id  
      ORDER BY lesson_id, chapters.id) as foo 
group by  chapter_id;

How, could I convert this query to use join instead of subquery.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安人多梦 2024-10-24 18:04:48

使用:

  SELECT c.id as chapter_id,
         COUNT(*) as num_of_questions 
    FROM CHAPTERS AS c
    JOIN LESSONS AS l ON l.chapter_id = c.id
    JOIN QUESTIONS AS q ON q.lesson_id = l.id
GROUP BY c.id

不需要子查询——只需放宽 GROUP BY 子句即可。

原始查询中的 ORDER BY 对您没有任何作用,浪费资源 - 除非您使用 LIMIT。

Use:

  SELECT c.id as chapter_id,
         COUNT(*) as num_of_questions 
    FROM CHAPTERS AS c
    JOIN LESSONS AS l ON l.chapter_id = c.id
    JOIN QUESTIONS AS q ON q.lesson_id = l.id
GROUP BY c.id

There's no need for a subquery -- just relax the GROUP BY clause.

The ORDER BY in your original query does nothing for you, wastes resources -- unless you use LIMIT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文