我需要从表tblResults中找出通过率最高的课程。

tblResults:
StuID       Course         Symbol
1001        CSC101         P
1001        RNG101         F
1002        CSC101         P
1002        RNF101         F
1003        HAP101         P
1004        HAP101         P


即应给CSC101(以及所有其他课程(HAP101)具有相同的通过率)

我试过了:

CREATE VIEW Part1 AS
SELECT NumbF
FROM
  (SELECT COUNT(Course) AS NumbP,
          Course
   FROM tblResults
   WHERE Symbol = 'P')
GROUP BY Course);


CREATE VIEW Part2 AS
SELECT MAX(NumbP) AS Maxnum
FROM Part1,
     tblResults
WHERE Symbol = 'P'
GROUP BY Course;


SELECT Part1.Course
FROM Part1,
     Part2
WHERE Part1.NumbP = Part2.MaxNum


但是我似乎做错了什么。请帮忙

最佳答案

这样的事情应该起作用:

create view yourview as
select course, count(*) passcnt
from tblResults
where symbol = 'P'
group by course

select *
from yourview
where passcnt = (select max(passcnt) from yourview)



SQL Fiddle Demo


请注意,您不需要该视图,为简单起见,我仅将其保留。

关于mysql - SQL比较两个必须确定的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23680679/

10-13 06:17