Laravel 5 - 即使条目不存在,如何从表中检索每个日期的结果

时间:2017-11-16 01:55:05

标签: php mysql sql laravel laravel-query-builder

我有一个名为daily_data的表,其中包含date字段。我希望能够通过Query Builder接口返回一系列日期中每一行的结果,即使给定日期没有条目(在这种情况下返回空值)

从挖掘中,一个建议似乎是生成范围内的动态日期列表,然后与表进行连接。这似乎是解决问题的一种相当不错的方法,但我在Laravel中找不到任何这方面的例子。我更希望从查询生成器返回完整的结果集,而不必在PHP中循环遍历结果,在查询后检测/添加空字段。

如果使用Laravel / Query Builder有更好的方法来实现这一点,我很乐意听到它。

与此同时,我发现此代码用于在SqlFiddle上生成一系列日期:http://sqlfiddle.com/#!9/38f7db/92。它做了它声称的,并且我确实使它适应在Laravel工作。它基本上是DB :: select中包含的所有内容。以下是我尝试将其与联接结合以获得我正在寻找的结果:

    $dates = DB::select(DB::raw(
        'SELECT a.Date as Date FROM
          (SELECT "2017-11-15" - INTERVAL (a.a + (10 * b.a)) DAY AS Date
           FROM
             (SELECT 0 AS a
              UNION ALL SELECT 1
              UNION ALL SELECT 2
              UNION ALL SELECT 3
              UNION ALL SELECT 4
              UNION ALL SELECT 5
              UNION ALL SELECT 6
              UNION ALL SELECT 7
              UNION ALL SELECT 8
              UNION ALL SELECT 9) AS a CROSS
           JOIN
             (SELECT 0 AS a
              UNION ALL SELECT 1
              UNION ALL SELECT 2
              UNION ALL SELECT 3) AS b) a
        where a.Date between "2017-11-01" and "2017-11-15"'))
        ->leftJoin(
            'daily_data',
            function($join) {
                $join->on("Date", "=", "daily_data.date");
            }
        );

我收到错误:

  

[2017-11-16 01:23:16] local.ERROR:Symfony \ Component \ Debug \ Exception \ FatalThrowableError:调用数组上的成员函数leftJoin()

我可以理解这不会像使用DB::table那样有效......但是有不同的方法可以完成我在这里尝试做的事情吗?

我已经验证生成日期的查询部分确实可以返回适当的日期列表。 (参见下面的代码,基本上与上面没有连接的相同,以及它给出的输出)。

   $dates = DB::select(DB::raw(
        'SELECT a.Date as Date FROM
          (SELECT "2017-11-15" - INTERVAL (a.a + (10 * b.a)) DAY AS Date
           FROM
             (SELECT 0 AS a
              UNION ALL SELECT 1
              UNION ALL SELECT 2
              UNION ALL SELECT 3
              UNION ALL SELECT 4
              UNION ALL SELECT 5
              UNION ALL SELECT 6
              UNION ALL SELECT 7
              UNION ALL SELECT 8
              UNION ALL SELECT 9) AS a CROSS
           JOIN
             (SELECT 0 AS a
              UNION ALL SELECT 1
              UNION ALL SELECT 2
              UNION ALL SELECT 3) AS b) a
        where a.Date between "2017-11-01" and "2017-11-15" '));

和输出......(2017-11-01和2017-11-15之间的日期列表):

[0] => stdClass Object
    (
        [Date] => 2017-11-15
    )
[1] => stdClass Object
    (
        [Date] => 2017-11-05
    )
[2] => stdClass Object
    (
        [Date] => 2017-11-14
    )
...

(我知道它没有订购,但我相信我可以解决这个问题,现在只想尝试解决基本想法)

提前感谢任何指示。

1 个答案:

答案 0 :(得分:0)

您可以尝试此原始查询,而不是使用leftJoin()方法

$dates = DB::select(DB::raw('
    SELECT a.Date AS DATE
    FROM
    (
        SELECT "2017-11-15" - INTERVAL (a.a + (10 * b.a)) DAY AS DATE
        FROM
        (
            SELECT 0 AS a UNION ALL
            SELECT 1 UNION ALL
            SELECT 2 UNION ALL
            SELECT 3 UNION ALL
            SELECT 4 UNION ALL
            SELECT 5 UNION ALL
            SELECT 6 UNION ALL
            SELECT 7 UNION ALL
            SELECT 8 UNION ALL
            SELECT 9) AS a CROSS
            JOIN
            (
                SELECT 0 AS a UNION ALL
                SELECT 1 UNION ALL
                SELECT 2 UNION ALL
                SELECT 3
            ) AS b
        ) a
    LEFT JOIN daily_data c ON a.Date=c.date
    WHERE a.Date BETWEEN "2017-11-01" AND "2017-11-15"
    ORDER BY a.Date'));