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?
謝謝。
哇,這很痛苦看 – 2013-04-15 02:47:15
是的..我知道,但它的工作原理,但我想知道一個更好的方式,所以而不是隻是要求,並沒有提供任何代碼,至少我做了第一次。 –
很好的讚揚,但我不知道什麼樣的期望輸出是基於該代碼,你不能在查詢中一天一組嗎? – 2013-04-15 03:05:39