在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...
正如你所看到的,三行總和相當於當天的每一個獨特行。
這可能嗎?
我明白,懷疑......你不應該只是接受成長率... ...但2的剩餘未接受值得接受(http://stackoverflow.com/questions/4830261/i-want-an-auto-hard-refresh-not-a-simple-auto-refresh-on-my-webpages/6938551#6938551)...接受並不意味着你專門使用了這個解決方案,但它也幫助其他人尋找答案,看看哪個答案是好的。 – Yahia
好點!我也接受了這個答案,謝謝你的提示Yahia;) – javipas
:-)看到我的回答如下 – Yahia