1

我有以下(缩短的查询):

SELECT 
    `Statistics`.`StatisticID`,
    COUNT(DISTINCT `Flags`.`FlagType`) AS `FlagCount`
FROM `Statistics`
LEFT JOIN `Flags` ON `Statistics`.`StatisticID` = `Flags`.`StatisticID`
WHERE `FlagCount` = 0
GROUP BY `Statistics`.`StatisticID`
ORDER BY `SubmittedTime` DESC
LIMIT 0, 10

现在,在子句中都没有FlagCount = 0COUNT(Flags.FlagType)工作。WHERE我考虑过使用 aSET但我不确定如何将它添加到查询中。有任何想法吗?

谢谢,

4

4 回答 4

5

使用HAVING COUNT(DISTINCT Flags.FlagType) = 0.

于 2008-11-23T11:22:37.167 回答
2

如果 HAVING 不起作用,也许您可​​以尝试子查询。

SELECT 
    `Statistics`.`StatisticID`,
    COUNT(DISTINCT `Flags`.`FlagType`) AS `FlagCount`
FROM `Statistics`
    LEFT JOIN `Flags` ON `Statistics`.`StatisticID` = `Flags`.`StatisticID`
WHERE `Statistics`.`StatisticID`
  IN (SELECT `Flags`.`StatisticID` 
      FROM `Flags`
      HAVING COUNT(DISTINCT `Flags`.`FlagType`) <= 3
      GROUP BY `Flags`.`StatisticID`
  )
GROUP BY `Statistics`.`StatisticID`
ORDER BY `SubmittedTime` DESC
LIMIT 0, 10
于 2008-11-23T12:40:17.570 回答
0

试试这个:

SELECT 
    `Statistics`.`StatisticID`,
    COUNT(DISTINCT `Flags`.`FlagType`) AS `FlagCount`
FROM `Statistics`
LEFT JOIN `Flags` ON `Statistics`.`StatisticID` = `Flags`.`StatisticID`
                     And `FlagCount` = 0
GROUP BY `Statistics`.`StatisticID`
ORDER BY `SubmittedTime` DESC
LIMIT 0, 10
于 2008-11-23T11:29:02.283 回答
0

@eed3si9n

这部分有效 - 但是我需要它是 <= 3 这似乎不起作用。

此外,该HAVING子句最后执行,它不会返回我需要的结果(由 设置LIMIT)。有没有办法我可以在一个WHERE子句中做到这一点?

于 2008-11-23T12:04:35.177 回答