0

鉴于此查询:

SELECT DISTINCT a.dogTag, a.dateEdited
FROM dvdpedia a
JOIN dvd_uploads b ON a.dogTag = b.dogTag
JOIN dvd_moderators c ON a.dogTag = c.dogTag
WHERE b.uploader != 9  AND c.moderator != 9
ORDER BY a.dogTag ASC
LIMIT 50;

我只想从 dvd_moderators 中选择仅出现一次的行,即仅被审核一次而不是多次且与给定的用户 ID 不匹配的行(本例中为 9)。

dvd_moderators看起来像这样:

id    dogTag    moderator
219    2         9
2226   2         7

当然,版主 ID 会因其他条目而改变。

使用 COUNT 最有效,除非用户 ID 与主持人匹配:

SELECT  a.dogTag, a.dateEdited, COUNT(c.dogTag) AS moderations
FROM dvdpedia a
JOIN dvd_uploads b ON a.dogTag = b.dogTag
JOIN dvd_moderators c ON a.dogTag = c.dogTag
WHERE b.uploader != 9 AND c.moderator != 9
GROUP BY c.dogTag
HAVING moderations = 1  
ORDER BY a.dogTag ASC
LIMIT 50;

然后它报告它只被审核了一次(这是一个正确的查询,它正在丢弃与 moderator = 9 匹配的行,因此计数变为 1 而不是 2)但对于我需要的目的不正确。

基本上,我想要只审核一次的行,并且其中一位审核员与用户 ID 不匹配(在上面的示例中为 9)。

我怎么能做到这一点?

谢谢。

4

2 回答 2

3

感谢 Sitepoint 的 Guido,这是他提供的解决方案。我把它放在这里是希望它能帮助处于类似情况的其他人:

SELECT a.dogTag, a.dateEdited
FROM dvdpedia a
JOIN dvd_uploads b ON a.dogTag = b.dogTag
JOIN dvd_moderators c ON a.dogTag = c.dogTag
JOIN
 (SELECT dogTag
  FROM dvd_moderators
  GROUP BY dogTag
  HAVING count(dogTag) = 1
 ) AS d
ON a.dogTag = d.dogTag
WHERE 1=1 
AND   b.uploader != 9 
AND   c.moderator != 9
ORDER BY a.dogTag ASC
LIMIT 50;

如果有人感兴趣,可以链接到该帖子。

于 2012-09-19T14:13:43.037 回答
0
SELECT  a.dogTag, a.dateEdited, COUNT(c.dogTag) AS moderations 
FROM dvdpedia a 
JOIN dvd_uploads b ON a.dogTag = b.dogTag AND b.uploader != 9
JOIN dvd_moderators c ON a.dogTag = c.dogTag AND c.moderator != 9
GROUP BY c.dogTag 
HAVING moderations = 1   
ORDER BY a.dogTag ASC 
LIMIT 50; 
于 2012-09-19T11:37:22.553 回答