2013-04-15 70 views
0

我有一個條形圖來填補這個星期一到星期天每週的每一天的7個總和1。PHP mysql查詢來填充條形圖最近7天

週一是工作周的開始,週日是本週的最後一天。

數據庫中的數據如下所示。

user1|2013-04-13 16:53:55|1 
user1|2013-03-13 16:53:55|1 
user1|2013-03-13 15:53:55|1 
user1|2013-02-13 09:53:55|1 
user1|2013-04-13 07:53:55|1 
user2|2013-03-13 15:53:55|1 

行數是爲每個用戶

總和下面是找出一週中的天的方法。

//find day of week 
    $dayofweek = date('l', strtotime(date("Y-m-d H:i:s"))); 

    $dates = array(); 
    if ($dayofweek == "Monday") { 
     $dates['monday']['morning'] = date('Y-m-d 00:00:01', strtotime('Monday last week')); 
     $dates['monday']['evening']= date('Y-m-d 23:59:59', strtotime('Monday last week')); 
     $dates['tuesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Tuesday this week')); 
     $dates['tuesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Tuesday this week')); 
     $dates['wednesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Wednesday this week')); 
     $dates['wednesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Wednesday this week')); 
     $dates['thursday']['morning'] = date('Y-m-d 00:00:01', strtotime('Thursday this week')); 
     $dates['thursday']['evening'] = date('Y-m-d 23:59:59', strtotime('Thursday this week')); 
     $dates['friday']['morning'] = date('Y-m-d 00:00:01', strtotime('Friday this week')); 
     $dates['friday']['evening'] = date('Y-m-d 23:59:59', strtotime('Friday this week')); 
     $dates['saturday']['morning'] = date('Y-m-d 00:00:01', strtotime('Saturday this week')); 
     $dates['saturday']['evening'] = date('Y-m-d 23:59:59', strtotime('Saturday this week')); 
     $dates['sunday']['morning'] = date('Y-m-d 00:00:01', strtotime('Sunday this week')); 
     $dates['sunday']['evening'] = date('Y-m-d 23:59:59', strtotime('Sunday this week'));  
    } 
    elseif ($dayofweek == "Tuesday") { 
     $dates['monday']['morning'] = date('Y-m-d 00:00:01', strtotime('Monday last week')); 
     $dates['monday']['evening']= date('Y-m-d 23:59:59', strtotime('Monday last week')); 
     $dates['tuesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Tuesday last week')); 
     $dates['tuesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Tuesday last week')); 
     $dates['wednesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Wednesday this week')); 
     $dates['wednesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Wednesday this week')); 
     $dates['thursday']['morning'] = date('Y-m-d 00:00:01', strtotime('Thursday this week')); 
     $dates['thursday']['evening'] = date('Y-m-d 23:59:59', strtotime('Thursday this week')); 
     $dates['friday']['morning'] = date('Y-m-d 00:00:01', strtotime('Friday this week')); 
     $dates['friday']['evening'] = date('Y-m-d 23:59:59', strtotime('Friday this week')); 
     $dates['saturday']['morning'] = date('Y-m-d 00:00:01', strtotime('Saturday this week')); 
     $dates['saturday']['evening'] = date('Y-m-d 23:59:59', strtotime('Saturday this week')); 
     $dates['sunday']['morning'] = date('Y-m-d 00:00:01', strtotime('Sunday this week')); 
     $dates['sunday']['evening'] = date('Y-m-d 23:59:59', strtotime('Sunday this week'));  
    } 
    elseif ($dayofweek == "Wednesday") { 
     $dates['monday']['morning'] = date('Y-m-d 00:00:01', strtotime('Monday last week')); 
     $dates['monday']['evening']= date('Y-m-d 23:59:59', strtotime('Monday last week')); 
     $dates['tuesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Tuesday last week')); 
     $dates['tuesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Tuesday last week')); 
     $dates['wednesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Wednesday last week')); 
     $dates['wednesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Wednesday last week')); 
     $dates['thursday']['morning'] = date('Y-m-d 00:00:01', strtotime('Thursday this week')); 
     $dates['thursday']['evening'] = date('Y-m-d 23:59:59', strtotime('Thursday this week')); 
     $dates['friday']['morning'] = date('Y-m-d 00:00:01', strtotime('Friday this week')); 
     $dates['friday']['evening'] = date('Y-m-d 23:59:59', strtotime('Friday this week')); 
     $dates['saturday']['morning'] = date('Y-m-d 00:00:01', strtotime('Saturday this week')); 
     $dates['saturday']['evening'] = date('Y-m-d 23:59:59', strtotime('Saturday this week')); 
     $dates['sunday']['morning'] = date('Y-m-d 00:00:01', strtotime('Sunday this week')); 
     $dates['sunday']['evening'] = date('Y-m-d 23:59:59', strtotime('Sunday this week'));  
    } 
    elseif ($dayofweek == "Thursday") { 
     $dates['monday']['morning'] = date('Y-m-d 00:00:01', strtotime('Monday last week')); 
     $dates['monday']['evening']= date('Y-m-d 23:59:59', strtotime('Monday last week')); 
     $dates['tuesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Tuesday last week')); 
     $dates['tuesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Tuesday last week')); 
     $dates['wednesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Wednesday last week')); 
     $dates['wednesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Wednesday last week')); 
     $dates['thursday']['morning'] = date('Y-m-d 00:00:01', strtotime('Thursday last week')); 
     $dates['thursday']['evening'] = date('Y-m-d 23:59:59', strtotime('Thursday last week')); 
     $dates['friday']['morning'] = date('Y-m-d 00:00:01', strtotime('Friday this week')); 
     $dates['friday']['evening'] = date('Y-m-d 23:59:59', strtotime('Friday this week')); 
     $dates['saturday']['morning'] = date('Y-m-d 00:00:01', strtotime('Saturday this week')); 
     $dates['saturday']['evening'] = date('Y-m-d 23:59:59', strtotime('Saturday this week')); 
     $dates['sunday']['morning'] = date('Y-m-d 00:00:01', strtotime('Sunday this week')); 
     $dates['sunday']['evening'] = date('Y-m-d 23:59:59', strtotime('Sunday this week'));  
    } 
    elseif ($dayofweek == "Friday") { 
     $dates['monday']['morning'] = date('Y-m-d 00:00:01', strtotime('Monday last week')); 
     $dates['monday']['evening']= date('Y-m-d 23:59:59', strtotime('Monday last week')); 
     $dates['tuesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Tuesday last week')); 
     $dates['tuesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Tuesday last week')); 
     $dates['wednesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Wednesday last week')); 
     $dates['wednesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Wednesday last week')); 
     $dates['thursday']['morning'] = date('Y-m-d 00:00:01', strtotime('Thursday last week')); 
     $dates['thursday']['evening'] = date('Y-m-d 23:59:59', strtotime('Thursday last week')); 
     $dates['friday']['morning'] = date('Y-m-d 00:00:01', strtotime('Friday last week')); 
     $dates['friday']['evening'] = date('Y-m-d 23:59:59', strtotime('Friday last week')); 
     $dates['saturday']['morning'] = date('Y-m-d 00:00:01', strtotime('Saturday this week')); 
     $dates['saturday']['evening'] = date('Y-m-d 23:59:59', strtotime('Saturday this week')); 
     $dates['sunday']['morning'] = date('Y-m-d 00:00:01', strtotime('Sunday this week')); 
     $dates['sunday']['evening'] = date('Y-m-d 23:59:59', strtotime('Sunday this week'));  
    } 
    elseif ($dayofweek == "Saturday") { 
     $dates['monday']['morning'] = date('Y-m-d 00:00:01', strtotime('Monday last week')); 
     $dates['monday']['evening']= date('Y-m-d 23:59:59', strtotime('Monday last week')); 
     $dates['tuesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Tuesday last week')); 
     $dates['tuesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Tuesday last week')); 
     $dates['wednesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Wednesday last week')); 
     $dates['wednesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Wednesday last week')); 
     $dates['thursday']['morning'] = date('Y-m-d 00:00:01', strtotime('Thursday last week')); 
     $dates['thursday']['evening'] = date('Y-m-d 23:59:59', strtotime('Thursday last week')); 
     $dates['friday']['morning'] = date('Y-m-d 00:00:01', strtotime('Friday last week')); 
     $dates['friday']['evening'] = date('Y-m-d 23:59:59', strtotime('Friday last week')); 
     $dates['saturday']['morning'] = date('Y-m-d 00:00:01', strtotime('Saturday last week')); 
     $dates['saturday']['evening'] = date('Y-m-d 23:59:59', strtotime('Saturday last week')); 
     $dates['sunday']['morning'] = date('Y-m-d 00:00:01', strtotime('Sunday this week')); 
     $dates['sunday']['evening'] = date('Y-m-d 23:59:59', strtotime('Sunday this week'));  
    } 
    elseif ($dayofweek == "Sunday") { 
     $dates['monday']['morning'] = date('Y-m-d 00:00:01', strtotime('Monday last week')); 
     $dates['monday']['evening']= date('Y-m-d 23:59:59', strtotime('Monday last week')); 
     $dates['tuesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Tuesday last week')); 
     $dates['tuesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Tuesday last week')); 
     $dates['wednesday']['morning'] = date('Y-m-d 00:00:01', strtotime('Wednesday last week')); 
     $dates['wednesday']['evening'] = date('Y-m-d 23:59:59', strtotime('Wednesday last week')); 
     $dates['thursday']['morning'] = date('Y-m-d 00:00:01', strtotime('Thursday last week')); 
     $dates['thursday']['evening'] = date('Y-m-d 23:59:59', strtotime('Thursday last week')); 
     $dates['friday']['morning'] = date('Y-m-d 00:00:01', strtotime('Friday last week')); 
     $dates['friday']['evening'] = date('Y-m-d 23:59:59', strtotime('Friday last week')); 
     $dates['saturday']['morning'] = date('Y-m-d 00:00:01', strtotime('Saturday last week')); 
     $dates['saturday']['evening'] = date('Y-m-d 23:59:59', strtotime('Saturday last week')); 
     $dates['sunday']['morning'] = date('Y-m-d 00:00:01', strtotime('Sunday last week')); 
     $dates['sunday']['evening'] = date('Y-m-d 23:59:59', strtotime('Sunday last week'));  
    } 

我應該爲每週的每一天創建7個查詢1嗎?

SELECT myDateTime FROM `mylog` WHERE myDateTime >= $dates['monday']['morning'] AND myDateTime <= $dates['monday']['evening'] 

還是有更簡單更有效的方法嗎?

謝謝。編輯1 ==================== ========

這是我到目前爲止。

SELECT date(myDateTime) , sum(myValue) 
FROM mylog 
GROUP BY date(myDateTime) 

如何指定我想要的星期一的日期?

有沒有辦法讓它變成1查詢而不是7?

謝謝。

+0

哇,這很痛苦看 – 2013-04-15 02:47:15

+0

是的..我知道,但它的工作原理,但我想知道一個更好的方式,所以而不是隻是要求,並沒有提供任何代碼,至少我做了第一次。 –

+0

很好的讚揚,但我不知道什麼樣的期望輸出是基於該代碼,你不能在查詢中一天一組嗎? – 2013-04-15 03:05:39

回答

0

我想你想要的可能是這個。雖然我可能是錯的

SELECT user, DAYOFWEEK(myDateTime) as day_of_week, SUM(myValue) as total 
FROM mylog 
WHERE myDateTime >= DATE_FORMAT(NOW() - INTERVAL 1 WEEK, "%Y-%m-%d") 
GROUP BY user, DAYOFWEEK(myDateTime) 

希望這是相當不言自明的? WHERE子句爲您提供上週的所有記錄,而GROUP BY將首先按用戶分組,然後是星期幾(1 =星期日)。手冊是http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_dayofweek這個東西。所以你應該最終得到類似

user | day_of_week | total 
------+---------------+------- 
user1 | 1    | 23 
user1 | 2    | 14 
... 
user1 | 7    | 65 
user2 | 1    | 12 
user2 | 2    | 78 

等等。這似乎是你一開始就問的問題?