【问题标题】:Laravel where clause with DATEDIFF()带有 DATEDIFF() 的 Laravel where 子句
【发布时间】:2017-11-23 09:39:47
【问题描述】:

我需要获取 DATEDDIFF() >30 和

我尝试过查询,但它不起作用。

如何获取符合条件的数据。

我的查询是,

 $cur_date=Carbon::now();


 $data=DB::table('mailbox_log as ml')
->leftjoin('registration as r','ml.reg_id','=','r.id')
->leftjoin('company as cmp','r.sociale_id','=','cmp.id')
->select('ml.*','r.id','r.g_id','r.num','cmp.name')
->where(DB::raw('DATEDIFF(ml.sent_pst_date, "%Y-%m-%d")'),$cur_date->format('Y-m-d'),'>','30')
->where(DB::raw('DATEDIFF(ml.sent_pst_date, "%Y-%m-%d")'),$cur_date->format('Y-m-d'),'<','60')
->get()->toArray();

【问题讨论】:

    标签: mysql database laravel datediff


    【解决方案1】:

    试试 Carbon addDays()

     $start_date = (Carbon::now())->addDays(30);
     $end_date = $start_date->addDays(30);
    
     $data=DB::table('mailbox_log as ml')
    ->leftjoin('registration as r','ml.reg_id','=','r.id')
    ->leftjoin('company as cmp','r.sociale_id','=','cmp.id')
    ->select('ml.*','r.id','r.g_id','r.num','cmp.name')
    ->where('ml.sent_pst_date','>',$start_date)
    ->where('ml.sent_pst_date','<',$end_date)
    ->get()->toArray();
    

    【讨论】:

      【解决方案2】:

      你应该试试这个:

      请使用-&gt;whereRaw() 而不是-&gt;where()

      $cur_date=Carbon::now();
      
      
       $data=DB::table('mailbox_log as ml')
      ->leftjoin('registration as r','ml.reg_id','=','r.id')
      ->leftjoin('company as cmp','r.sociale_id','=','cmp.id')
      ->select('ml.*','r.id','r.g_id','r.num','cmp.name')
      ->whereRaw(DB::raw('DATEDIFF(ml.sent_pst_date,$cur_date->format("Y-m-d")','=','30'))
      ->whereRaw(DB::raw('DATEDIFF(ml.sent_pst_date, "%Y-%m-%d")'),$cur_date->format('Y-m-d'),'<','60')
      ->get()->toArray();
      

      【讨论】:

        【解决方案3】:

        我使用 whereRaw(Eloquent)&& Carbon(日期),例如:

        $cur_date=Carbon::now();
        
        
         $data=DB::table('mailbox_log as ml')
        ->leftjoin('registration as r','ml.reg_id','=','r.id')
        ->leftjoin('company as cmp','r.sociale_id','=','cmp.id')
        ->select('ml.*','r.id','r.g_id','r.num','cmp.name')
        ->whereRaw("DATEDIFF('" . Carbon::now() . "',created_at)  > 30")
        ->whereRaw("DATEDIFF('" . Carbon::now() . "',created_at)  < 60")
        ->get()->toArray();
        

        【讨论】:

          【解决方案4】:

          使用 DATE_FORMAT 和 CURDATE();

          例子:

          whereRaw('DATEDIFF(CURDATE(),DATE_FORMAT(created_at,"%Y-%m-%d")) > 30 AND 
          DATEDIFF(CURDATE(),DATE_FORMAT(created_at,"%Y-%m-%d")) < 60 ');
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-09-05
            • 1970-01-01
            • 2019-10-15
            • 2017-06-29
            • 2011-07-23
            • 2011-08-02
            • 1970-01-01
            相关资源
            最近更新 更多