首先,预先感谢您的帮助。这将是我对SOF的第一个问题。

我有以下SQL数据库表。

qualificationTable

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


testTable

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


我分别为课程A,B,C和D指定了最低百分比。我需要寻找符合所有课程最低标准的学生。

第2部分:
该学生还应匹配testTable中的标准(分别针对三个测试1,2和3的最低百分比)。

换句话说,如果学生符合针对所有课程分别指定的最低标准(百分比),则应选择该学生。现在,testTable也是如此,该特定学生(在qualificationTable中被选中)应该具有为testNo列中的三个测试(1,2和3)分别指定的最低标准(百分比)。

编辑:

我已经更新了testTable,现在针对特定学生有多个测试。我需要检查学生是否满足所有3个测试中指定的最低要求百分比,但是,每个数字(1、2和3)中只有最近参加的测试才算在内。如果学生不符合为最新考试指定的最低标准,则不应将其包括在内。

测试用例:

要求的最低资格百分比:


  路线A:90路线B:80路线C:70路线D:60


所需的最低测试百分比:


  测试1:90测试2:80测试3:70


预期产量

studentNo

1


干杯

最佳答案

我已经为您的样本数据和测试用例弄清楚了:


  要求的最低资格百分比:
  
  
    路线A:90路线B:80路线C:70路线D:60
  
  
  所需的最低测试百分比:
  
  
    测试1:90测试2:80测试3:70
  


试试这个,可能对您有帮助;)

SQL Fiddle

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可以给我们这样的结果,


    | 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 run having 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.

Results:

| 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

10-08 14:27