-2

I have records like this:

id | name | date
1  | aji  | 2014-06-01
2  | akon | 2014-06-02
3  | dewi | 2014-06-04
4  | dani | 2014-06-05

I want to display all dates from 2014-06-01 - 2014-06-06, even if they have no records associated:

id   | name | date
1    | aji  | 2014-06-01
2    | akon | 2014-06-02
NULL | NULL | 2014-06-03
3    | dewi | 2014-06-04
4    | dani | 2014-06-05
NULL | NULL | 2014-06-06
SandPiper
  • 2,816
  • 5
  • 30
  • 52

3 Answers3

1

Possibly you can use this way.

SQL Server

Select IsNull(id, Null), IsNull(name,Null),date 
   where date Between  2014-06-01 and 2014-06-06.

If ID has set to int, then do like this

Select IsNull(id, 0), IsNull(name,Null),date 
   where date Between  2014-06-01 and 2014-06-06.

MY SQL

select id, name,date from tr_kehadiran 
    where date >= "2014-06-30" AND date <= "2014-07-06" AND id is Null AND name is Null

Please let me know the result.

gkrishy
  • 756
  • 1
  • 8
  • 33
1

I'm not sure i understand what you're asking for. But

SELECT * FROM table WHERE date BETWEEN  '2014-06-01' AND '2014-06-06'

And

<?php
$start = new DateTime('2014-06-01');
$interval = new DateInterval('P1D');
$end = new DateTime('2014-06-06');

$period = new DatePeriod($start, $interval, $end);

foreach ($period as $date)
{
    if (isset($results['date'][$date]))
    {
        // Results display
    }
    else
    {
        // NULL display
    }
}

where $results is the result of your query

Betezed
  • 11
  • 2
0

Following are the steps to be followed -

If SQL Server

  1. Using CTE, populate a range of date needed as per your requirement

  2. Left join your table against the CTE on the date column and populate the dates missing in your result set from CTE

If MySQL -

  1. Create a temporary table to populate the date range as per the requirement

  2. Left join the temporary table on the date column and populate the dates missing in your result set from CTE

Paul Roub
  • 36,322
  • 27
  • 84
  • 93