【发布时间】:2014-02-05 20:56:04
【问题描述】:
我有 2 个表格,如下所示:
TABLE 1 TABLE 2
user_id | date accountID | date | hours
我正在尝试按周计算小时数。如果我使用以下语句,我会得到正确的结果:
SELECT
SUM(hours) as totalHours
FROM
hours
WHERE
accountID = 244
AND
date >= '2014-02-02' and date < '2014-02-09'
GROUP BY
accountID
但是当我加入这两个表时,我会得到一个像 336640 这样的数字,而它应该是 12
SELECT
SUM(hours) as totalHours
FROM
hours
JOIN table1 ON
user_id = accountID
WHERE
accountID = 244
AND
date >= '2014-02-02' and date < '2014-02-09'
GROUP BY
accountID
有人知道这是为什么吗?
编辑:原来我只需要添加 DISTINC,谢谢!
【问题讨论】:
-
这是正确的
date >= '2014-02-02' and date < '2014-02-09'不在 parentethis 之间吗? -
将它们放在括号之间并没有改变任何东西
-
你能解释一下你想要达到的结果吗?
-
请提供一些示例数据。