考虑如下中间结果...
SELECT s.id
, s.member_group
, j.judge_id
, j.stage_1
, j.stage_2
FROM exp_judging j
JOIN exp_submissions s
ON s.id = j.rel_id
AND j.stage_1 = 1
AND s.member_group = 5
ORDER
BY j.rel_id
, j.judge_id;
+----+--------------+----------+---------+---------+
| id | member_group | judge_id | stage_1 | stage_2 |
+----+--------------+----------+---------+---------+
| 70 | 5 | 14 | 1 | 5 |<-- exclude because [14,5]
| 70 | 5 | 16 | 1 | NULL |<-- exclude because of [14,5] above
| 73 | 5 | 14 | 1 | 5 |<-- exclude because [14,5]
| 73 | 5 | 15 | 1 | NULL |<-- exclude because [14,5] above
| 73 | 5 | 16 | 1 | NULL |<-- exclude because [14,5] above
| 75 | 5 | 15 | 1 | NULL |
| 75 | 5 | 16 | 1 | NULL |
| 77 | 5 | 15 | 1 | NULL |
| 77 | 5 | 16 | 1 | NULL |
| 78 | 5 | 16 | 1 | NULL |
| 79 | 5 | 14 | 1 | NULL |
| 80 | 5 | 14 | 1 | NULL |
| 80 | 5 | 15 | 1 | NULL |
| 80 | 5 | 16 | 1 | NULL |
| 81 | 5 | 16 | 1 | NULL |
| 83 | 5 | 14 | 1 | NULL |
| 83 | 5 | 15 | 1 | NULL |
+----+--------------+----------+---------+---------+
要排除突出显示的行,我们可以这样做......
SELECT s.id
, s.member_group
, j.judge_id
, j.stage_1
, j.stage_2
FROM exp_judging j
JOIN exp_judging x
ON x.rel_id = j.rel_id
AND x.judge_id = 14
AND x.stage_2 IS NULL
JOIN exp_submissions s
ON s.id = j.rel_id
AND j.stage_1 = 1
AND s.member_group = 5
GROUP
BY j.rel_id
, j.judge_id;
+----+--------------+----------+---------+---------+
| id | member_group | judge_id | stage_1 | stage_2 |
+----+--------------+----------+---------+---------+
| 75 | 5 | 15 | 1 | NULL |
| 75 | 5 | 16 | 1 | NULL |
| 77 | 5 | 15 | 1 | NULL |
| 77 | 5 | 16 | 1 | NULL |
| 78 | 5 | 16 | 1 | NULL |
| 79 | 5 | 14 | 1 | NULL |
| 80 | 5 | 14 | 1 | NULL |
| 80 | 5 | 15 | 1 | NULL |
| 80 | 5 | 16 | 1 | NULL |
| 81 | 5 | 16 | 1 | NULL |
| 83 | 5 | 14 | 1 | NULL |
| 83 | 5 | 15 | 1 | NULL |
+----+--------------+----------+---------+---------+
从这里开始,剩下的步骤是微不足道的......
SELECT s.id
, COUNT(*)
FROM exp_judging j
JOIN exp_judging x
ON x.rel_id = j.rel_id
AND x.judge_id = 14
AND x.stage_2 IS NULL
JOIN exp_submissions s
ON s.id = j.rel_id
AND j.stage_1 = 1
AND s.member_group = 5
GROUP
BY j.rel_id;
+----+----------+
| id | COUNT(*) |
+----+----------+
| 75 | 2 |
| 77 | 2 |
| 78 | 1 |
| 79 | 1 |
| 80 | 3 |
| 81 | 1 |
| 83 | 2 |
+----+----------+