7

以下查询计算了 exp_judging 表中有多少行,字段 pre 中的值为 1,其中 exp_submissions 中还有一个匹配列,其 member_group 为 5

SELECT COUNT(*) AS count_result
  FROM exp_judging AS jud
  LEFT JOIN exp_submissions AS sub ON jud.rel_id = sub.id 
  WHERE jud.pre = 1
        AND sub.member_group = 5

这很好用。但是,将来在 'exp_judging' 表中会有多个具有相同 'rel_id' 的行。

如何确保每行只计算一个?

我尝试将 GROUP BY 添加到末尾,但它只显示 1 个结果,而不是之前的 4 个。(还没有重复)

GROUP BY jud.rel_id

编辑:

尝试了以下方法,但仍然只显示 1:

SELECT COUNT(DISTINCT jud.rel_id) AS count_result
FROM exp_judging AS jud
LEFT JOIN exp_submissions AS sub ON jud.rel_id = sub.id 
WHERE jud.pre = 1
AND sub.member_group = $member_group
GROUP BY jud.rel_id
4

2 回答 2

23

使用COUNT(DISTINCT rel_id)而不是COUNT(*).

于 2013-10-03T07:12:40.743 回答
0

试试下面的查询

 SELECT COUNT(jud.rel_id) AS count_result
 FROM exp_judging AS jud
 LEFT JOIN exp_submissions AS sub ON jud.rel_id = sub.id 
 WHERE jud.pre = 1
    AND sub.member_group = 5
group by jud.rel_id
于 2013-10-03T07:20:51.927 回答