[英]How to join two queries in SQL from 3 tables?
I have 3 tables (M:N) - Tests, Tests_Questions, Questions. 我有3个表格(M:N)-测试,Tests_Questions,问题。
Tests 测试
ID
Name
Test_Questions Test_Questions
IDTests
IDQuestions
Questions 问题
ID
Text
What I need is select all from tests and count of question related to this test. 我需要从测试中选择所有内容,并选择与此测试相关的问题数。
Can you please help me with this query? 您能帮我这个疑问吗? I am not able to solve it. 我无法解决。
Use join and grouping: 使用联接和分组:
SELECT t.ID, t.Name, COUNT(tq.IDQuestions) as numberOfQuestions
FROM Tests t INNER JOIN Test_Questions tq ON t.ID = tq.IDTests
GROUP BY t.ID, t.Name
Since you only need counts it is enough to join just Tests
and Test_Questions
tables, you don't need Questions
由于您只需要计数就可以只连接Tests
和Test_Questions
表,因此您不需要Questions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.