mysql按周显示记录

时间:2011-03-04 12:41:04

标签: mysql database

嗨我有一个订单表我想运行一个查询,如果在任何一周都没有订单,那么在过去4周内将不返回订单,它应该说0订单我正在做什么不起作用

ID | order_Date | amount
1  | 2011-03-01  | 10
2  | 2011-03-01  | 50
3  | 2011-02-24  | 60

select sum(amount) as total from orders group by WEEK(order_date,INTERVAL 3 Week)

感谢

3 个答案:

答案 0 :(得分:0)

select monday,monday + interval 6 day as sunday,coalesce(a.total,0) as total from (
select curdate() - 
interval weekday(curdate()) day - interval tmp.digit * 1 week as monday             
from (
select 0 as digit union all 
select 1 union all 
select 2 union all 
select 3
) as tmp ) as t 
left join (     select 
        week(order_date) as nweek, 
        sum(amount) as total
        from orders
        where order_date
        between 
        curdate() - interval weekday(curdate()) day - interval 3 week and
        curdate() + interval 6 - weekday(curdate()) day
        group by nweek order by null
       ) as a
on week(monday) = a.nweek   
group by a.nweek
order by monday desc

结果:

+------------+------------+--------+
| monday     | sunday     | total  |
+------------+------------+--------+
| 2011-02-28 | 2011-03-06 | 834312 |
| 2011-02-21 | 2011-02-27 | 818334 |
| 2011-02-14 | 2011-02-20 | 824032 |
| 2011-02-07 | 2011-02-13 | 695021 |
+------------+------------+--------+

答案 1 :(得分:0)

让我们来看看你正在使用的语言:

  

将在过去4周内退回订单

这里你宣布一个条件,在这个条件下,行应该计入总和。这些条件在sql中表示为“where”子句。所以我们的“where”子句在这种情况下应该比较订单的日期,并看到它在一定的时间间隔内。

现在的问题是,如果您的案件中有4周意味着“本周和本周前的三周”,或“今天和今天之前的27天”。使用“27天”推理最容易,所以我会用它!因此,我们的标准是订单日期(在列order_Date中找到)应该 之后<或>(或大于)今天前27天的日期。这意味着我们需要计算在这一天之前27天的一天。我们使用函数SUBDATE和函数CURDATE来完成此操作。这里只是“where”条款:

WHERE
    order_Date > SUBDATE(CURDATE(), INTERVAL 27 DAY)
通过这种方式,我们可以找到想要一起添加的所有订单。这是列出我们想要的所有订单的查询:

SELECT
    *
FROM
    orders
WHERE
    order_Date > SUBDATE(CURDATE(), INTERVAL 27 DAY);

现在我们需要将它们添加到一个结果行中!要做到这一点,我们需要将结果行合并为一行,然后总结所有金额。但是“分组依据”仅对所选列的分组具有相同值的行进行分组。并且无法保证线路中会有任何此类列。所以我们将在查询的SELECT部分​​创建一个。

SELECT
    amount,
    1 AS column_to_group_by
FROM
    orders
WHERE
    order_Date > SUBDATE(CURDATE(), INTERVAL 27 DAY);

现在我们可以按列column_to_group_by分组。

SELECT
    amount,
    1 AS column_to_group_by
FROM
    orders
WHERE
    order_Date > SUBDATE(CURDATE(), INTERVAL 27 DAY)
GROUP BY
    column_to_group_by;

结果现在完全没用,因为它只显示表格第一行的数量。但我们现在可以总结所有金额,这样就得到了我们想要的答案!

SELECT
    SUM(amount),
    1 AS column_to_group_by
FROM
    orders
WHERE
    order_Date > SUBDATE(CURDATE(), INTERVAL 27 DAY)
GROUP BY
    column_to_group_by;

我希望这个解释可以帮助您采用此处应用的问题解决方法:)

答案 2 :(得分:0)

select sum(amount) as total from orders 
where 
order_date between date_sub(now(), interval 4 week) and 
date_sub(now(), interval 3 week)
union
select sum(amount) as total from orders 
where 
order_date between date_sub(now(), interval 3 week) and 
date_sub(now(), interval 2 week)
union
select sum(amount) as total from orders 
where 
order_date between date_sub(now(), interval 2 week) and 
date_sub(now(), interval 1 week)
union
select sum(amount) as total from orders 
where 
order_date between date_sub(now(), interval 1 week) and now();