moh*_*abk 11 php laravel laravel-5
以下是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
Run Code Online (Sandbox Code Playgroud)
saturday_ot并且saturday_ct是TIME我表中的列,$t是一个时间变量.所以我想检查时间是否在两列中的时间之间.
Bog*_*dan 28
除了whereBetween适用于两列的方法之外别无选择.但是,您可以通过以下两种方式之一完成此操作:
1.whereRaw与绑定一起使用,使用原始条件和变量的绑定:
whereRaw('? between saturday_ot and saturday_ct', [$t])
Run Code Online (Sandbox Code Playgroud)
2.使用where两个条件,使用两个列值作为$t变量值的边界:
where(function ($query) use ($t) {
$query->where('saturday_ot', '<=', $t);
$query->where('saturday_ct', '>=', $t);
})
Run Code Online (Sandbox Code Playgroud)