我需要从我的表 tblResults 中找出通过次数最多的类(class)。
tblResults:
StuID Course Symbol
1001 CSC101 P
1001 RNG101 F
1002 CSC101 P
1002 RNF101 F
1003 HAP101 P
1004 HAP101 P
即应该给予CSC101(以及所有其他类(class)(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)
请注意,您不需要该 View ,为了简单起见,我只是保留了它。
关于mysql - SQL比较必须确定的2个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23680679/