【问题标题】:Laravel: Date between with zero valuesLaravel:零值之间的日期
【发布时间】:2015-03-18 22:12:00
【问题描述】:

我正在尝试获取给定日期范围的数据库值。问题是,我也在努力获得缺失值。如果我有第 1 天和第 3 天的值,并且我要求提供第 1-5 天的值,我想得到这样的结果:

1 -> 价值

2 -> NULL / 0

3 -> 价值

4 -> NULL / 0

5 -> NULL / 0

好的,我知道如何只使用 MySQL 和左连接中的这个临时表来解决这个问题:

(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from                                                                                            
    (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union  select 8 union select 9) t0,
    (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
    (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
    (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
    (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v

不幸的是,我不确定如何将它包含在 Eloquent ORM 中。 目前我正在考虑这样的事情:

$weekclockings = $shift->clockings()->leftJoin(function($query)
{                                                                   
    $query->raw($aboveQuery);
}, "v.selected_date", ">", "timeclocked.time_in")

但这不起作用,只会给我“Grammar.php 第 33 行中的 ErrorException:类 Closure 的对象无法转换为字符串”。

所以,这可能不是最好的方法。有谁知道如何实现这一目标?

【问题讨论】:

  • 我没有得到您将日期值与日期值混合(订单?)的部分。当允许 null 时,任何人如何区分应该放在范围内的内容?我的意思是,如果你知道你想要的日子,只需使用简单的 where 并订购它......

标签: php mysql laravel eloquent laravel-5


【解决方案1】:

我不知道这里的 clockings() 是什么 - 可能是一个范围,但要在连接中使用原始 SQL,您应该使用:

$weekclockings = $shift->clockings()->leftJoin(\DB::raw($aboveQuery), "v.selected_date", ">", "timeclocked.time_in");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2016-05-06
    • 2014-09-09
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    相关资源
    最近更新 更多