title: Select between two date-columns with interval
body: <p>I have a table with a column for a start datetime and a end datetime and a column (quantity) with a number. For a display of a diagram, I need a query that outputs the added values from the column "quantity" in intervals of 15 minutes with given a start and end datetime.</p>
  
  <p>Currently, I am solving this through a loop in PHP, creating 96 queries for a day. Very bad, I know.</p>
  
  <p>The table looks like this, for example:</p>
  
  <pre><code>id      from_datetime           until_datetime          quantity
  ----------------------------------------------------------------
  1       2018-10-30 08:00:00     2018-10-30 09:59:59     5
  2       2018-10-30 09:00:00     2018-10-30 10:59:59     1
  3       2018-10-30 10:30:00     2018-10-30 11:59:59     3
  4       2018-10-30 10:30:00     2018-10-30 14:59:59     3
  5       2018-10-30 11:00:00     2018-10-30 13:59:59     2
  </code></pre>
  
  <p>The result with given 2018-10-30 06:00:00 as start-datetime and 2018-10-30 16:00:00 should be the following:
  Timestamps without any results should be a line with zero as number.</p>
  
  <pre><code>timestamp               result  
  ------------------------------
  2018-10-30 06:00:00     0        
  2018-10-30 06:15:00     0        
  2018-10-30 06:30:00     0        
  2018-10-30 06:45:00     0        
  2018-10-30 07:00:00     0        
  2018-10-30 07:15:00     0        
  2018-10-30 07:30:00     0        
  2018-10-30 07:45:00     0        
  2018-10-30 08:00:00     5        
  2018-10-30 08:15:00     5        
  2018-10-30 08:30:00     5        
  2018-10-30 08:45:00     5        
  2018-10-30 09:00:00     6        
  2018-10-30 09:15:00     6        
  2018-10-30 09:30:00     6        
  2018-10-30 09:45:00     6        
  2018-10-30 10:00:00     1        
  2018-10-30 10:15:00     1        
  2018-10-30 10:30:00     7        
  2018-10-30 10:45:00     7        
  2018-10-30 11:00:00     8        
  2018-10-30 11:15:00     8        
  2018-10-30 11:30:00     8        
  2018-10-30 11:45:00     8        
  2018-10-30 12:00:00     5        
  2018-10-30 12:15:00     5        
  2018-10-30 12:30:00     5        
  2018-10-30 12:45:00     5        
  2018-10-30 13:00:00     5        
  2018-10-30 13:15:00     5        
  2018-10-30 13:30:00     5        
  2018-10-30 13:45:00     5        
  2018-10-30 14:00:00     3        
  2018-10-30 14:15:00     3        
  2018-10-30 14:30:00     3        
  2018-10-30 14:45:00     3        
  2018-10-30 15:00:00     0        
  2018-10-30 15:15:00     0        
  2018-10-30 15:30:00     0        
  2018-10-30 15:45:00     0        
  2018-10-30 16:00:00     0
  </code></pre>
  
  <p>My PHP-solution works but is very hungry and slow. certainly this can be solved with a single query, but I failed with many attempts.</p>
  
  <p>Cheers,
  Eric</p>
  
  <p>My php is a simple loop with php DateTime object:</p>
  
  <pre><code>$begin = new DateTime("2018-10-30 06:00:00"); 
  $end = new DateTime("2018-10-30 16:00:00");
  $interval = DateInterval::createFromDateString('15 minutes');
  $period = new DatePeriod($begin, $interval, $end);
  
  foreach ( $period as $dt ) {
      $check_from = new DateTime($dt-&gt;format("Y-m-d H:i"));
      $check_until = new Datetime($dt-&gt;format("Y-m-d H:i"));
  
      // MySql-Query and write Array with the data
  }
  </code></pre>