sql上周总计

时间:2013-09-26 17:56:30

标签: mysql sql

查找上周总计的查询

当天

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();

寻找类似于此的查询来计算前一周。似乎无法让它发挥作用。

2 个答案:

答案 0 :(得分:1)

可能你想要这个: -

SELECT SUM(total_cost)AS "total"
    FROM purchases
`datetime ` >= DATE_SUB(NOW(),INTERVAL 14 DAY) 
AND `datetime ` < DATE_SUB(NOW(),INTERVAL 7 DAY)

答案 1 :(得分:0)

select SELECT SUM(total_cost) AS "total"
  FROM purchases
  WHERE datetime between dateadd(d,-14,CURDATE()) and dateadd(d,-7,CURDATE())