将子查询转换为联接
我有三个表格,即章节、课程和问题,用于显示课程的问题。下面给出表结构供参考。
表“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用:
不需要子查询——只需放宽 GROUP BY 子句即可。
原始查询中的 ORDER BY 对您没有任何作用,浪费资源 - 除非您使用 LIMIT。
Use:
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.