在MySQL查询中使用DISTINCT,COUNT和HAVING

时间:2012-09-09 17:51:40

标签: mysql

我想计算在给定日期之后总和至少为300分的所有玩家。我有这个:

SELECT
    COUNT(DISTINCT playerID)
FROM
    table
WHERE
    game_date > '$date'
HAVING
    SUM(points) >= 300

还计算总和少于300分的球员。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

正如Andre Bossard所指出的那样:

获取点数> gt =所有玩家的列表:

SELECT * FROM (SELECT playerID, SUM(points) points FROM table WHERE game_date>'$date' GROUP BY playerID) a WHERE points>=300

要计算:

SELECT COUNT(*) FROM (SELECT playerID, SUM(points) points FROM table WHERE game_date>'$date' GROUP BY playerID) a WHERE points>=300

答案 1 :(得分:1)

使用子查询:

SELECT COUNT(*) FROM
(
  SELECT playerID, SUM(points) AS `total`
  FROM `table`
  WHERE game_date > '$date'
  GROUP BY playerID
) tmp
WHERE total>=300