代码之家  ›  专栏  ›  技术社区  ›  Grant

用逗号分隔的时间戳在哪?

  •  0
  • Grant  · 技术社区  · 6 年前

    enter image description here

    根据上面的图片…使用laravel的雄辩-我如何查找特定的日期范围以返回该范围内的行?

    我知道laravel有一个wherebetween函数,可以处理传统的日期字段,但是由于这些是时间戳,我意识到我必须使用 where('dates', '>=', $startTimestamp) where('dates', '<=', $endTimestamp) 但我也要把它应用到每一个从列中分解出来的日期上?

    谢谢你们的帮助!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Grant    6 年前

    尽管它可能是糟糕的数据库设置,但有时事情只能按照当前的方式来完成——出于业务原因或延迟或其他原因;难看的修复可能比全面彻底检查要好——特别是在系统很小并且仍将很小的情况下,不需要过多考虑处理速度。

    下面是一个解决方案,使用mysql表连接,从连接响应中选择。这也是用laravel编写的,因此是db static函数。

    $temp = DB::select(DB::raw("select
      leave_entries.id,
      SUBSTRING_INDEX(SUBSTRING_INDEX(leave_entries.dates, ',', numbers.n), ',', -1) dates
      from
      (select 1 n union all
       select 2 union all select 3 union all
       select 4 union all select 5) numbers INNER JOIN leave_entries
      on CHAR_LENGTH(leave_entries.dates)
         -CHAR_LENGTH(REPLACE(leave_entries.dates, ',', ''))>=numbers.n-1
         where uid = {$uid}
         AND dates >= {$start_timestamp} 
         AND dates <= {$end_timestamp}
      order by
      id, n"));
    
    // Return the resulting table:
    dd($temp);
    

    把这个留在这里而不是删除这个问题,因为它可能会帮助别人。

    未来的人:在你的情况下-如果你可以重做数据库模式,作为我的问题的评论建议,然后这样做。