2016-01-22 62 views
8

两列下面是从Laravel文档的摘录:SQL之间Laravel /流明

的whereBetween方法验证列的值是两个值之间:

$users = DB::table('users')->whereBetween('votes', [1, 100])->get();

但是如果我想知道数据库中两列之间是否有值,该怎么办?

这是我的原始SQL:

SELECT a.*, b.name FROM restaurants a, restaurant_class b 
WHERE a.restaurant_class_id = b.id 
AND '$d' = CURRENT_DATE 
AND '$t' BETWEEN a.saturday_ot AND a.saturday_ct 
ORDER BY id DESC 

saturday_otsaturday_ctTIME列在我的表和$t是一个时间变量。所以我想检查时间是否在两列中的时间之间。

+1

我认为你必须使用whereRaw方法 –

回答

17

对于适用于两列的whereBetween方法,没有其他选择。然而,你可以做到这一点在以下两种方法之一:

使用whereRaw绑定的,在您使用的原始条件和变量绑定:

whereRaw('? between saturday_ot and saturday_ct', [$t]) 

2.使用where有两个使用两个值作为边界的$t变量值条件:

where(function ($query) use ($t) { 
    $query->where('saturday_ot', '<=', $t); 
    $query->where('saturday_ct', '>=', $t); 
})