Laravel where子句与DATEDIFF()

时间:2017-11-23 09:39:48

标签: mysql database laravel datediff

我需要获取DATEDDIFF()&gt; 30和<60的所有记录。

我尝试了一个查询,但它无法正常工作。

我如何获得符合条件的数据。

我的查询是,

 $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();

4 个答案:

答案 0 :(得分:0)

你应该试试这个:

请使用->whereRaw()代替->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();

答案 1 :(得分:0)

尝试使用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 :(得分:0)

我使用 whereRaw (雄辩)和& Carbon (日期),例如:

fn get_mut_pair<'a, K, V>(conns: &'a mut HashMap<K, V>, a: &K, b: &K) -> (&'a mut V, &'a mut V)
where
    K: std::fmt::Debug + Eq + std::hash::Hash,
{
    unsafe {
        assert_ne!(a, b, "`a` ({:?}) must not equal `b` ({:?})", a, b);
        let a = conns.get_mut(a).unwrap() as *mut _;
        let b = conns.get_mut(b).unwrap() as *mut _;
        (&mut *a, &mut *b)
    }
}

答案 3 :(得分:0)

使用日期格式和CURRDATE;

EX:

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