我有一个名为 finished_ca 的表
表格是这样的:
id date_received manufacturating_date sample_status
1 2017-06-08T15:53 2017-07-05 1
2 2017-06-08T15:49 2017-08-01 2
如果 sample_status = 1 我必须添加的天数是 30 如果 sample_status = 2 我必须添加的天数是 14
现在我想获取所有 date_received 小于 manufacturing_date 的 id,如果 sample_id 是 1,我必须添加的天数是 30,如果是 2,我将添加 30
如果是用php语言写的,会是这样的:
这只是一个例子。
$days = sample_status == 1 ? 30 : 14;
继承人我试过的查询:
/*
I know that this will not work, but i want to parse the date first,
and add days depending on the sample_status
*/
$response_data = DB::table('finished_ca')
->where('date_received', '<=', Carbon::parse('manufacturing_date')->addDays(??));
最佳答案
sr,我没仔细看 这是我的更新答案:D
$response_data = DB::table('finished_ca')
->where(function($query){
$query->where('sample_status', 1)
->whereRaw('date(date_received) <= DATE_ADD(manufacturing_date, INTERVAL 30 DAY)');
})
->orWhere(function($query){
$query->where('sample_status', 2)
->whereRaw('date(date_received) <= DATE_ADD(manufacturing_date, INTERVAL 14 DAY)');
})->get();
关于php - Laravel DB - 通过在另一列上添加日期来比较 2 个日期列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45625858/