2011-10-31 65 views
2

previous question上可以創建一個表並在每月的幾天填充表,但是我希望該表的填充略有不同:每個月的每一天應該有三個不同的小時間隔。MySQL查詢中的三小時時間間隔

根據這個問題,這個代碼由Tom Mac

create table all_date 
(id int unsigned not null primary key auto_increment, 
a_date date not null, 
last_modified timestamp not null default current_timestamp on update current_timestamp, 
unique key `all_date_uidx1` (a_date)); 

然後,

DELIMITER // 


CREATE PROCEDURE populate_all_dates(IN from_date DATE, IN days_into_future INT) 

BEGIN 

DECLARE v_date DATE; 
DECLARE ix int; 


SET ix := 0; 
SET v_date := from_date; 


WHILE v_date <= (from_date + interval days_into_future day) DO 

    insert into all_date (a_date) values (v_date) 
    on duplicate key update last_modified = now(); 

    set ix := ix +1; 

    set v_date := from_date + interval ix day; 

END WHILE; 

END// 

DELIMITER ; 

然後你就可以運行:

call populate_all_dates('2011-10-01',30); 

填充所有日期爲十月(或者任何月份,改變函數的值)

有了,我可以運行下面的查詢

select day(a.a_date) as 'October', 
IFNULL(t.a1,0) as 'Auth1', 
IFNULL(t.a2,0) as 'Auth2', 
IFNULL(t.a50,0) as 'Auth50' 
from all_date a 
LEFT OUTER JOIN 
(
SELECT date(wp.post_date) as post_date, 
sum(case when wp.post_author = '1' then 1 else 0 end) as a1, 
sum(case when wp.post_author = '2' then 1 else 0 end) as a2, 
sum(case when wp.post_author = '50' then 1 else 0 end) as a50, 
count(*) as 'All Auths' 
FROM wp_posts wp 
WHERE wp.post_type = 'post' 
AND wp.post_date between '2011-10-01' and '2011-10-31 23:59:59' 
GROUP BY date(wp.post_date) 
) t 
ON a.a_date = t.post_date 
where a.a_date between '2011-10-01' and '2011-10-31' 
group by day(a.a_date); 

而且我會得到一個表,按作者和日,類似這樣在我的WordPress博客文章數量:

+---------+---------+-------+------+---------+ 
| October | Auth1 | Auth2 | Auth3| Auth4 | 
+---------+---------+-------+------+---------+ 
|  1 |  0 |  0 | 0 |  0 | 
|  2 |  0 |  0 | 1 |  0 | 
|  3 |  4 |  4 | 6 |  2 | 
|  4 |  4 |  3 | 5 |  2 | 
|  5 |  7 |  0 | 5 |  2 | 
|  6 |  4 |  4 | 0 |  2 | 
|  7 |  0 |  2 | 1 |  2 | 
|  8 |  0 |  0 | 7 |  0 | 
..... 
etc 

但我喜歡每天分成三個不同的行,每一行對應於以下時間範圍:

00:00-14:30 14:31-18:15 18:16 -23:59

所以表格應該顯示類似的內容(例如,我不知道如何顯示每個時間範圍,所以一個好的方法應該是第1天,時間範圍1(1-1 )等)。

+---------+---------+-------+------+---------+ 
| October | Auth1 | Auth2 | Auth3| Auth4 | 
+---------+---------+-------+------+---------+ 
| 1-1 |  0 |  0 | 0 |  0 | 
| 1-2 |  0 |  0 | 0 |  0 | 
| 1-3 |  0 |  0 | 0 |  0 | 
| 2-1 |  0 |  0 | 1 |  0 | 
| 2-2 |  0 |  0 | 0 |  0 | 
| 2-3 |  0 |  0 | 0 |  0 | 
| 3-1 |  1 |  2 | 3 |  0 | 
| 3-2 |  1 |  2 | 2 |  2 | 
| 3-3 |  2 |  0 | 1 |  0 | 
etc... 

正如你所看到的,三行總和相當於當天的每一個獨特行。

這可能嗎?

+0

我明白,懷疑......你不應該只是接受成長率... ...但2的剩餘未接受值得接受(http://stackoverflow.com/questions/4830261/i-want-an-auto-hard-refresh-not-a-simple-auto-refresh-on-my-webpages/6938551#6938551)...接受並不意味着你專門使用了這個解決方案,但它也幫助其他人尋找答案,看看哪個答案是好的。 – Yahia

+0

好點!我也接受了這個答案,謝謝你的提示Yahia;) – javipas

+0

:-)看到我的回答如下 – Yahia

回答

1

使用(UPDATE#2)

SELECT 
a.a_datetm as 'October', 
IFNULL(p.a1,0) as 'Auth1', 
IFNULL(p.a2,0) as 'Auth2', 
IFNULL(p.a50,0) as 'Auth50' 
FROM 
(
SELECT CONCAT (day(X.a_date), '-1') AS a_datetm 
FROM all_date X 
WHERE X.a_date between '2011-10-01' and '2011-10-31' 
UNION ALL 
SELECT CONCAT (day(Y.a_date), '-2') AS a_datetm 
FROM all_date Y 
WHERE Y.a_date between '2011-10-01' and '2011-10-31' 
UNION ALL 
SELECT CONCAT (day(Z.a_date), '-3') AS a_datetm 
FROM all_date Z 
WHERE Z.a_date between '2011-10-01' and '2011-10-31' 
) a 
LEFT OUTER JOIN 
(
SELECT 
CONCAT (day(wp.post_date), (CASE WHEN (TIME(wp.post_date) < '14:31:00') THEN '-1' WHEN (TIME(wp.post_date) BETWEEN '14:31:00' AND '18:15:59') THEN '-2' ELSE '-3' END)) AS a_datetm, 
sum(case when wp.post_author = '1' then 1 else 0 end) as a1, 
sum(case when wp.post_author = '2' then 1 else 0 end) as a2, 
sum(case when wp.post_author = '50' then 1 else 0 end) as a50, 
count(*) as 'All Auths' 
FROM wp_posts wp 
WHERE wp.post_type = 'post' 
AND wp.post_date between '2011-10-01' and '2011-10-31 23:59:59' 
GROUP BY CONCAT (day(wp.post_date), (CASE WHEN (TIME(wp.post_date) < '14:31:00') THEN '-1' WHEN (TIME(wp.post_date) BETWEEN '14:31:00' AND '18:15:59') THEN '-2' ELSE '-3' END)) 
) p 
ON a.a_datetm = p.a_datetm 
ORDER BY a.a_datetm ASC; 
+0

Yahia,它顯示這個錯誤:錯誤1064(42000):你的SQL語法錯誤;檢查對應於您的MySQL服務器版本的手冊,在'CASE'附近使用'10月', IFNULL(t.a1,0)作爲'Auth1', IFNULL(t.a2,0) Auth2', IFNULL'在第1行 我試過檢測錯誤,但找不到解決方法:( – javipas

+0

@javipas我沒有MySQL,但請嘗試上面的更改(添加了一些'( '並將'END CASE'改爲'END') – Yahia

+0

嗯,現在它可以工作,但它與原始查詢顯示的一樣,這是輸出(截圖更簡單,在這裏http:// imgur。com/mRfow): +---------+-------+-------+--------+ | October | Auth1 | Auth2 | Auth50 | +---------+-------+-------+--------+ | 1-1 | 0 | 0 | 0 | | 10-1 | 5 | 0 | 9 | | 11-1 | 1 | 0 | 10 | | 12-1 | 0 | 0 | 4 | ... 它沒有顯示,但第一個時間間隔(沒有1-2,1-3,2-2,2-3等),它包括了這些時間間隔中的所有帖子。所以計數不能正常工作:( – javipas