简体   繁体   中英

Display Every Day of the month even if no data is available

I want to display all the days of the month in a SQL statement and then link that day up with data in my table. If there is no data for that day then it must display a null.

My Table looks like this.

IN | OUT | EARN | DATE
10   20    0.25   01.02.2013
2    15    0.55   03.02.2013
7    45    0.25   05.02.2013
8    25    0.75   12.02.2013

I then want the result to look something like this

IN | OUT | EARN | DATE
10   20    0.25   01.02.2013
0    0     0.00   02.02.2013
2    15    0.55   03.02.2013
0    0     0.00   04.02.2013
7    45    0.25   05.02.2013
0    0     0.00   06.02.2013
0    0     0.00   07.02.2013
0    0     0.00   08.02.2013
0    0     0.00   09.02.2013
0    0     0.00   10.02.2013
0    0     0.00   11.02.2013
8    25    0.75   12.02.2013

all the way to the end of the month...

Please can you assist in this so that i can solve the report.

And my sql im get data in this way

$sql = "SELECT * FROM stats WHERE date >= '".$month_start."' AND date <= '".$month_end."' AND pid={$pid}";

Try this:

SELECT `t`.`IN`, `t`.`OUT`, `t`.`EARN`, `d`.`DATE`
FROM `table` AS `t`
RIGHT JOIN (
    SELECT @date := @date + 1 AS `DATE`
    FROM `tbl31`
    JOIN (SELECT @date := 0) AS `temp`
    LIMIT 31
) AS `d`
ON `d`.`DATE`=`t`.`DATE`

table is the name of your table.
tbl31 is a table which have more than 30 rows (the content is not important).

I don't really know a better way when using MySQL (I assume you use MySQL).

Iterate over all days:

$start  = new \DateTime('first day of this month');
$end    = new \DateTime('first day of this month + 1 month');
$period = new \DatePeriod($start, new \DateInterval('P1D'), $end);

foreach($period as $day){
  // here check if you have records with this date and print them,
  // otherwise print default values
  print $day->format('d.m.Y');
}

You can also iterate over the days of the month:

/*
    $r is something like:
    $r = array(
        array(
            'IN' => '10',
            'OUT' => '20',
            'EARN' => '0.25',
            'DATE' => '01.02.2013'
        ),
        array(
            'IN' => '2',
            'OUT' => '15',
            'EARN' => '0.55',
            'DATE' => '03.02.2013'
        ),
        array(
            'IN' => '7',
            'OUT' => '45',
            'EARN' => '0.25',
            'DATE' => '05.02.2013'
        ),
        array(
            'IN' => '8',
            'OUT' => '25',
            'EARN' => '0.75',
            'DATE' => '12.02.2013'
        )
    );
*/
$arr = array(); // the result array
$period = new DatePeriod(
    new DateTime('first day of this month'),
    new DateInterval('P1D'),
    new DateTime('first day of next month')
);
$ri = 0;
foreach ($period as $day) {
    $i = ((int)$day->format('j')) - 1;
    $date = $day->format('d.m.Y');
    $arr[$i] = array(
        'IN' => '0.00',
        'OUT' => '0.00',
        'EARN' => '0.00',
        'DATE' => $date
    );
    if (array_key_exists($ri, $r) && $r[$ri]['DATE'] == $date) {
        $arr[$i] = $r[$ri];
        $ri++;
    }
}

$arr is know like $r but contains all the days in the month.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM