首先,提前感谢您的帮助。这是我关于 SOF 的第一个问题。
我有以下 SQL 数据库表。
资格表
:
QualId studentNo CourseName Percentage
1 1 A 91
2 1 B 81
3 1 C 71
4 1 D 61
5 2 A 91
6 2 B 81
7 2 C 71
8 2 D 59
测试表
:
TestId studentNo testNo Percentage dateTaken
1 1 1 91 2016-05-02
2 1 2 41 2015-05-02
3 1 3 71 2016-04-02
4 1 1 95 2014-05-02
5 1 2 83 2016-01-02
6 1 3 28 2015-05-02
7 2 1 90 2016-05-02
8 2 2 99 2016-05-02
9 2 3 87 2016-05-02
我分别为类(class) A、B、C 和 D 指定了最低百分比。我需要搜索满足所有类(class)最低标准的学生。
第 2 部分: 该学生还应该符合 testTable 中的标准(为三个测试分别指定的最低百分比 - 1,2 和 3)。
换句话说,如果学生符合为所有类(class)单独指定的最低标准(百分比),则应选择他。现在,测试表也是如此,该特定学生(在资格表中被选中的)应该在 testNo 列中为三个测试(1,2 和 3)单独指定最低标准(百分比)。
编辑:
我已经更新了测试表,现在某个特定学生有多个测试。我需要检查学生是否满足所有 3 项测试指定的最低要求百分比,但是,只有每项(1,2 和 3)中最近参加的测试才应计算在内。如果学生不符合最近测试指定的最低标准,则不应将其纳入其中。
测试用例:
所需的最低资格百分比:
Course A: 90 Course B: 80 Course C: 70 Course D: 60
所需的最低测试百分比:
Test 1: 90 Test 2: 80 Test 3: 70
预期输出
studentNo
1
干杯
最佳答案
我刚刚计算出了您的示例数据和测试用例:
Minimum qualification percentage required:
Course A: 90 Course B: 80 Course C: 70 Course D: 60
所需的最低测试百分比:
block 引用>Test 1: 90 Test 2: 80 Test 3: 70
试试这个,可能对你有帮助;)
MySQL 架构:
CREATE TABLE qualificationTable (`QualId` int, `studentNo` int, `CourseName` varchar(1), `Percentage` int) ; INSERT INTO qualificationTable (`QualId`, `studentNo`, `CourseName`, `Percentage`) VALUES (1, 1, 'A', 91), (2, 1, 'B', 81), (3, 1, 'C', 71), (4, 1, 'D', 61), (5, 2, 'A', 91), (6, 2, 'B', 81), (7, 2, 'C', 71), (8, 2, 'D', 50) ; CREATE TABLE testTable (`TestId` int, `studentNo` int, `testNo` int, `Percentage` int) ; INSERT INTO testTable (`TestId`, `studentNo`, `testNo`, `Percentage`) VALUES (1, 1, 1, 91), (2, 1, 2, 81), (3, 1, 3, 71), (4, 2, 1, 80), (5, 2, 2, 99), (6, 2, 3, 87) ;
查询 1:
select t1.studentNo from ( select studentNo from qualificationTable where (CourseName = 'A' and Percentage >= 90) or (CourseName = 'B' and Percentage >= 80) or (CourseName = 'C' and Percentage >= 70) or (CourseName = 'D' and Percentage >= 60) group by studentNo having count(1) = 4 ) t1 join ( select studentNo from testTable where (testNo = '1' and Percentage >= 90) or (testNo = '2' and Percentage >= 80) or (testNo = '3' and Percentage >= 70) group by studentNo having count(1) = 3 ) t2 on t1.studentNo = t2.studentNo
我只选
t1
这两个子查询之一解释其工作原理:
GROUP BY
可以得到这样的结果,| studentNo | |-----------| | 1 | | 2 |
COUNT
will get us total count of each group, for your sample data, studentNo(1) is 4, studentNo(2) is 4 as well, but we also has where clause here, so by these criteria, we can find which matched are following record,(1, 1, 'A', 91), (2, 1, 'B', 81), (3, 1, 'C', 71), (4, 1, 'D', 61), (5, 2, 'A', 91), (6, 2, 'B', 81), (7, 2, 'C', 71)
- And this means
COUNT
will give us studentNo(1) to 4, studentNo(2) to 3, so when mysql runhaving count(1) = 4
, this subquery only return us studentNo(1)Subquery
t2
works like that, and when join these two subquery by studentNo, it will return what you expected result.| studentNo | |-----------| | 1 |
已编辑:
select t1.studentNo from ( select studentNo from qualificationTable where (CourseName = 'A' and Percentage >= 90) or (CourseName = 'B' and Percentage >= 80) or (CourseName = 'C' and Percentage >= 70) or (CourseName = 'D' and Percentage >= 60) group by studentNo having count(1) = 4 ) t1 join ( select studentNo from ( select * from testTable where (testNo, dateTaken) in ( select testNo, Max(dateTaken) from testTable group by testNo ) ) tmp where (testNo = '1' and Percentage >= 90) or (testNo = '2' and Percentage >= 80) or (testNo = '3' and Percentage >= 70) group by studentNo having count(1) = 3 ) t2 on t1.studentNo = t2.studentNo
关于mysql - SQL 中同一列中多个值的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37160135/