1

查找对前一周总计的查询

当前日期

SELECT Sum(total_cost)AS "total" 
FROM   purchases 
WHERE  datetime >= Curdate();

本周

SELECT SUM(total_cost)as "total" 
FROM purchases 
WHERE datetime >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK);

前一天

SELECT SUM(total_cost)AS "total"
FROM purchases
WHERE datetime >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)
  AND datetime <= CURDATE();

寻找与此类似的查询以计算前一周。似乎无法让它工作。

4

2 回答 2

1

可能你想要这个:-

SELECT SUM(total_cost)AS "total"
    FROM purchases
`datetime ` >= DATE_SUB(NOW(),INTERVAL 14 DAY) 
AND `datetime ` < DATE_SUB(NOW(),INTERVAL 7 DAY)
于 2013-09-26T17:58:35.673 回答
0
select SELECT SUM(total_cost) AS "total"
  FROM purchases
  WHERE datetime between dateadd(d,-14,CURDATE()) and dateadd(d,-7,CURDATE())
于 2013-09-26T18:17:20.153 回答