如何在子查询中计数

How to count in a sub-query

这就是我的代码。

SELECT TestName
FROM Testtype
WHERE TestID IN (Select TestID
                    from Tests
                    where Testno IN (Select Testno
                                      from Users
                                      WHERE StudentType= 'StudentType1'
                                      )
                    )

这只会生成与学生类型匹配的测试名称列表 所以喜欢

Test1
Test2

我想要这样我可以计算 test1 有多少匹配所以输出就像

No of student that undertook the test   Test Type
52                                      Test1
23                                      Test2

感谢任何帮助,谢谢。

用联接替换子查询,并添加 GROUP BY:

SELECT tt.TestName, COUNT(*) as NoOfStudents
FROM TestType tt
JOIN Tests t ON tt.TestID=t.TestID 
JOIN Users u ON t.Testno=u.Testno AND u.StudentType= 'StudentType1'
GROUP BY tt.TestName

您也可以使用此查询。

SELECT TestName,count(*) StudentsCount
FROM Testtype tt,
     Tests t,
     Users usr
where usr.StudentType='StudentType1'
  and usr.Testno = t.Testno
  and t.TestID=tt.TestID
group by TestName