doulu5717 2017-05-24 10:40
浏览 269
已采纳

MySQL - INTERVAL 24小时不按预期工作

I am building a chart and for that extracting data from the past 24 hours, 48 hours, 1 week and 2 weeks.

I used INTERVAL statements for this purpose but they are not working as expected. I am using Laravel. Here is the function to extract the data:

public function range($range)
{
    $data = new Main();
    $data0 = $data->whereRaw('updated_at >= DATE_SUB(NOW(), INTERVAL '.$range.')')->whereRaw('MINUTE(updated_at)>54')->orwhereRaw('MINUTE(updated_at)<6')->where('server_short_name', '=', 'FiveRP')->get();
    $data1 = $data->whereRaw('updated_at >= DATE_SUB(NOW(), INTERVAL '.$range.')')->whereRaw('MINUTE(updated_at)>54')->orwhereRaw('MINUTE(updated_at)<6')->where('server_short_name', '=', 'GTALife')->get();
    $data2 = $data->whereRaw('updated_at >= DATE_SUB(NOW(), INTERVAL '.$range.')')->whereRaw('MINUTE(updated_at)>54')->orwhereRaw('MINUTE(updated_at)<6')->where('server_short_name', '=', 'GermanV')->get();
    return compact('data0', 'data1', 'data2');
}

Here is the function that calls the view:

    public function hours24()
{
    $t = $this::range('24 HOUR');
    return view('chart', $t);
}

But in the chart, I am getting results from 22nd of May as well which is the earliest date on DB. Which means that the code isnt working as expected although it is correct according to me. Is there something I am missing out?

  • 写回答

1条回答 默认 最新

  • dousilie9522 2017-05-24 10:50
    关注

    I have found the solution. Actually in my code I am using an 'orwhereRaw' which means that I am implying an OR between all the where statements which is sadly not what I want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)