1

我有这个工作正常的查询。它从两个表 manager_details 和 users 中获取数据。我需要的是从第 1 周到第 7 周带来经理分数的总和。Week 是 manager_details 表中的另一个字段,它的值可以是 0-x。

SELECT
  SUM(mng.score) AS accumulated_score,
  mng.manager_id AS manager_id,
  users.id,
  users.fname,
  users.lname
FROM manager_details AS mng
  JOIN users
    ON users.id = mng.manager_id
GROUP BY manager_id
ORDER BY accumulated_score DESC
LIMIT 5

为了从第 1 周到第 7 周获得分数,我尝试添加

SELECT
  SUM(mng.score) AS accumulated_score,
  mng.manager_id AS manager_id,
  users.id,
  users.fname,
  users.lname
FROM manager_details AS mng
WHERE  mng.week >= 1 AND mng.week <= 6    -- this line
  JOIN users
    ON users.id = mng.manager_id
GROUP BY manager_id
ORDER BY accumulated_score DESC
LIMIT 5

得到了这个

错误代码:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 'join users on users.id = mng.manager_id GROUP BY manager_id 附近使用的正确语法

也试过

     ...         
     HAVING mng.week >= 1 AND mng.week <= 6  -- <-- 
     ...

我还将 HAVING 类放在 GROUP BY 之后,GROUP BY 之前,最后并没有工作,得到 mysql 错误检查语法..等等。

我做错了什么,但不确定是什么。

那么,要使用什么子句,我在哪里设置该条件来过滤我的 mng.score 并仅获得位于第 1-7 周之间的分数?

4

1 回答 1

2
SELECT
  SUM(mng.score) AS accumulated_score,
  mng.manager_id AS manager_id,
  users.id,
  users.fname,
  users.lname
FROM manager_details AS mng
  JOIN users
    ON users.id = mng.manager_id
WHERE  mng.week >= 1 AND mng.week <= 6  
GROUP BY manager_id
ORDER BY accumulated_score DESC
LIMIT 5
于 2012-11-01T04:01:06.730 回答