查询和分组

时间:2014-04-10 20:43:47

标签: mysql sql

我和日期和时间以及考官有一些会议,在这些会议上,我提出了一个候选人。考生必须通过考试。

我有一张这样的表:

Exam | Meeting | Niche | Date     | Hour | Examinator | idCandidate
Math |    1    |   1   |2014-06-28|08:00 |    John    |     1
Math |    1    |   2   |2014-06-28|08:20 |    John    |     0
Math |    5    |   3   |2014-06-28|13:00 |    John    |     0
Math |    2    |   4   |2014-06-28|08:00 |    Will    |     0
Math |    2    |   5   |2014-06-28|08:20 |    Will    |     0
Math |    3    |   6   |2014-06-29|08:00 |    John    |     11
Math |    3    |   7   |2014-06-29|08:20 |    John    |     0
Math |    6    |   8   |2014-06-29|14:00 |    John    |     0
Bio  |    4    |   9   |2014-06-28|08:00 |    Bruce   |     0
Bio  |    4    |   10  |2014-06-28|08:20 |    Bruce   |     0
Bio  |    4    |   11  |2014-06-28|08:40 |    Bruce   |     0

我想要的是:

Exam |   Date   | Examinator | Nb Free Candidate
Math |2014-06-28|  John      |         2
Math |2014-06-28|  Will      |         2
Math |2014-06-29|  John      |         2
Bio  |2014-06-28|  Bruce     |         3

实际上,我想通过考试,日期和考试来显示免费小众(会议)顺序的数量。

你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您可能正在寻找此GROUP BY查询:

SELECT Exam, `Date`, Examinator, COUNT(*) AS nb_free_candidate
FROM tablename
WHERE idCandidate = 0
GROUP BY Exam, `Date`, Examinator

如果您的日期列包含日期和时间,则应使用此代码:

SELECT Exam, DATE(`Date`), Examinator, COUNT(*) AS nb_free_candidate
FROM tablename
WHERE idCandidate = 0
GROUP BY Exam, DATE(`Date`), Examinator