以下是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_ot
和 saturday_ct
是我的表中TIME
列,$t
是时间变量。所以我想检查时间是否在两列的时间之间。
除了
适用于两列的 whereBetween
方法之外,没有其他方法。但是,您可以通过以下两种方式之一执行此操作:
1. 将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);
})