如何获取未在数据库表中添加的完整月份日期?

时间:2017-09-19 10:06:13

标签: php mysql mysqli

您好我有一个sql查询返回以下数据,如下图所示。 我的Sql查询是

SELECT datei, sum(amount)
FROM
(
 SELECT datei, amount, 1 AS identification FROM income
 UNION ALL
 SELECT datee, amount, 2 AS identification FROM expense
 UNION ALL
 SELECT date, amount, 3 AS identification FROM others
) t
GROUP BY datei
ORDER BY datei ASC

结果是

Click Here For Image

这给我一个客户注册日期。 我需要整个月的所有日期。在数据库中跳过。 作为图像中的示例在2017/09/04之后的下一个日期是2017/09/06所以05日期被跳过。

根据图像中显示的示例,我需要在01到12天之间的所有日期。 我怎样才能使用PHP?

2 个答案:

答案 0 :(得分:1)

您需要外部联接表或选择包含该月中所有日期的表。 Friddle example

select date(concat('2017-09-',  d)) AS dt
from (
    select 1 AS d
    union select 2
    union select 3
    union select 4
    union select 5
    union select 6
    union select 7
    union select 8
    union select 9
    union select 10
    union select 11
    union select 12
    union select 13
    union select 14
    union select 15
    union select 16
    union select 17
    union select 18
    union select 19
    union select 20
    union select 21
    union select 22
    union select 23
    union select 24
    union select 25
    union select 26
    union select 27
    union select 28
    union select 29
    union select 30
    union select 31
) as t
having dt is not null

答案 1 :(得分:-1)

这样的事情应该有效,但尚未经过测试

$app->get( '/test/', function () use($app) {
        $app->view = new Json();
        try{
            //here the output of Json view
            $model=["myjsondata"=>[]];
            $app->render(200,$model);
        }catch (\Slim\Exception\Stop $e) {}

        //following code is copied from Slim->run() to early output
        $app->response()->headers->replace(["Content-Length"=>$app->response()->length()]);
        $app->response()->headers->replace(["Connection"=>"close"]);
        list($status, $headers, $body) = $app->response->finalize();
        \Slim\Http\Util::serializeCookies($headers, $app->response->cookies, $app->settings);
        if (headers_sent() === false) {
            if (strpos(PHP_SAPI, 'cgi') === 0) {
                header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
            } else {
                header(sprintf('HTTP/%s %s', $app->config('http.version'), \Slim\Http\Response::getMessageForCode($status)));
            }
            foreach ($headers as $name => $value) {
                $hValues = explode("\n", $value);
                foreach ($hValues as $hVal) {
                    header("$name: $hVal", false);
                }
            }
        }
        if (!$app->request->isHead()) {
            echo $body;
        }

        //early output to client
        ob_end_flush();
        ob_flush();
        flush();
        if (session_id()) session_write_close();

        //my async job
        sleep(5);
    });