嗨我有一個訂單表我想運行一個查詢,將返回在過去4周沒有訂單,如果沒有訂單的任何一週它應該說0順序我正在做的是不工作mysql顯示週記錄
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)
感謝
嗨我有一個訂單表我想運行一個查詢,將返回在過去4周沒有訂單,如果沒有訂單的任何一週它應該說0順序我正在做的是不工作mysql顯示週記錄
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)
感謝
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 |
+------------+------------+--------+
讓我們看一下你使用的語言:
,將不返回訂單在過去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;
我希望這個解釋將幫助您採取解決問題的方法應用在這裏:)
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();
還有什麼你試過嗎? – awm 2011-03-04 12:43:04
itried YEARWEEK,但它只是顯示我單個記錄,然後 – r1400304 2011-03-04 12:46:31
你知道「where子句」是如何工作的嗎? :) – davogotland 2011-03-04 12:56:30