Laravel/Lumen 中两列之间的 SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34950852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:05:09  来源:igfitidea点击:

SQL BETWEEN Two Columns in Laravel/Lumen

phplaravellaravel-5

提问by moh_abk

Below is an excerpt from the Laravel documentation:

以下是 Laravel 文档的摘录:

The whereBetween method verifies that a column's value is between two values:

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

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

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

But what if I want to find out if a value is between two columns in my database?

但是如果我想知道一个值是否在我的数据库中的两列之间呢?

This is my raw SQL:

这是我的原始 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_otand saturday_ctare TIMEcolumns in my table and $tis a time variable. So I want to check if the time is in between the the times in both columns.

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

回答by Bogdan

There is no alternative to the whereBetweenmethod that applies to two columns. You can however do this in one of two ways:

whereBetween适用于两列的方法没有替代方法。但是,您可以通过以下两种方式之一执行此操作:

1.Use whereRawwith bindings, where you use the raw condition and a binding for the variable:

1.whereRaw与绑定一起使用,在其中使用原始条件和变量的绑定:

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

2.Use a wherewith two conditions that use the two column values as boundaries for the $tvariable value:

2.将 awhere与两个条件一起使用,使用两个列值作为$t变量值的边界:

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