添加过滤器到我的查询导致错误。 Mysql的

时间:2012-11-01 03:47:11

标签: mysql

我有这个查询工作正常。它带来了两个表,manager_details和用户的数据。我需要的是让经理总数从第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服务器版本   在'加入用户附近'使用的语法       on users.id = mng.manager_id GROUP BY manager_id

并尝试了

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

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

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

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

1 个答案:

答案 0 :(得分: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