0

月曜日から日曜日までの曜日ごとに 7 つの合計 1 を求める棒グラフがあります。

月曜日が週の始まりで、日曜日が週の最後の日です。

データベース内のデータは次のようになります。

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 )

月曜日のように日付を指定するにはどうすればよいですか?

これを 7 つではなく 1 つのクエリにする方法はありますか?

ありがとう。

4

1 に答える 1