0

表:

exp_submissions
-----------------------------------------------------------------------------------
id | entry_id | member_id | member_group | category_id | type_id | portfolio_number
exp_judging
------------------------------------------------
id | rel_id | judge_id | pre | stage_1 | stage_2

目的:

  • 将两个表连接在一起exp_judging.rel_id, 和exp_submissions.id
  • 仅选择以下行:
    • exp_submissions.member_group = 5exp_judging.pre = 1
    • judge_id != 1, 或者judge_id IS NULL
    • 不存在具有相同rel_id和的现有行judge_id = 1

以下查询执行所有操作,直到最后一部分:

SELECT sub.entry_id 
FROM exp_judging AS jud
LEFT JOIN exp_submissions AS sub ON jud.rel_id = sub.id 
WHERE (jud.judge_id != 1 OR jud.judge_id IS NULL)
AND jud.pre = 1
AND sub.member_group = 5
LIMIT 1

这些是我在 exp_judging 表中的测试行:

在此处输入图像描述

正如您将看到的,有两行rel_id = 35(ID 200 和 197)。上面的查询正在选择行 id 197,即使有另一行具有相同的行rel_id并且具有judge_id = 1. 这是我的问题。

我需要添加到我的查询中以检查是否不存在具有相同rel_idand的其他行judge_id = 1,但我不知道如何。

谢谢你。

4

1 回答 1

1

你应该使用一个NOT EXISTS子句:

   SELECT sub.entry_id 
     FROM exp_judging AS jud
LEFT JOIN exp_submissions AS sub ON jud.rel_id = sub.id 
    WHERE (jud.judge_id != 1 OR jud.judge_id IS NULL)
      AND jud.pre = 1
      AND sub.member_group = 5
      AND NOT EXISTS (SELECT sub2.entry_id 
                        FROM exp_judging AS jud2
                   LEFT JOIN exp_submissions AS sub2 ON jud2.rel_id = sub2.id 
                       WHERE (jud2.judge_id = 1 )
                         AND jud2.pre = 1
                         AND sub2.member_group = 5
                         AND jud2.rel_id = jud.rel_id)
    LIMIT 1

编辑:添加了一个新条件

于 2013-10-04T07:46:19.163 回答