MySQL:如何根据WHERE子句的结果计算ORDERBY子句的和
我有下表的捐款信息:MySQL:如何根据WHERE子句的结果计算ORDERBY子句的和,mysql,sql,Mysql,Sql,我有下表的捐款信息: +-------------+------------+--------+ | supporterID | date | amount | +-------------+------------+--------+ | 1 | 2018-01-01 | 100 | | 2 | 2018-01-07 | 10 | | 3 | 2018-01-09 | 5000 | | 1
+-------------+------------+--------+
| supporterID | date | amount |
+-------------+------------+--------+
| 1 | 2018-01-01 | 100 |
| 2 | 2018-01-07 | 10 |
| 3 | 2018-01-09 | 5000 |
| 1 | 2018-02-01 | 100 |
| 2 | 2018-02-03 | 10 |
| 1 | 2018-02-10 | 100 |
| 2 | 2018-03-14 | 10 |
| 3 | 2018-03-18 | 5 |
+-------------+------------+--------+
我需要选择从2月1日起按捐款总额排序的支持者,然后按total
value排序。忽略2月1日之前的所有捐款
我的非执行查询是:
SELECT supporterID, sum(amount) AS total
FROM donations
WHERE date >= 2018-02-01
GROUP BY supporter_id
ORDER BY total desc
LIMIT 10
问题是,sum(amount)
是根据所有的捐款计算的,而不仅仅是从2月1日起的捐款supporterID 3
作为最高结果返回,因为他们在一月份捐赠了5000英镑
结果应该是:
+-------------+-------+
| supporterID | total |
+-------------+-------+
| 1 | 200 |
| 2 | 20 |
| 3 | 5 |
+-------------+-------+
我怎样才能做到这一点呢?根据,问题是我缺少引号,而不是查询本身
以下是工作查询:
SELECT supporterID, sum(amount) AS total
FROM donations
WHERE date >= '2018-02-01'
GROUP BY supporter_id
ORDER BY total desc
LIMIT 10
在
2018-02-01
周围使用引号。否则的话,这个对比就是2018-2-1=2015。啊,谢谢,你说得对!我试过了,但它不让我这么做,因为人们已经对它进行了评论和编辑