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.

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

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?