我需要在Laravel中转换以下查询。有人可以帮忙吗?
select *
from testtable
where (pttid NOT IN 5 OR (pttid = 6 AND is != 1))
AND (sod BETWEEN '2019-01-01' AND '2019-01-01' || updat BETWEEN '2019-01-01' AND '2019-01-01'))
答案 0 :(得分:0)
假设您有一个TestTable
模型,则查询可以是:
$result = TestTable::select(['*'])
->where(function($q) {
$q->where('pttid', 'NOT IN', 5);
$q->orWhere(function($q2) {
$q2->where('pttid', '=', 6);
$q2->where('is', '!=', 1);
});
})->where(function ($q3) {
$q3->whereBetween('sod', ['2019-01-01', '2019-01-01']);
$q3->orWhereBetween('updat', ['2019-01-01', '2019-01-01']);
})->get();
答案 1 :(得分:0)
以最干净的方式执行此操作:
TestTable
模型:命令
php artisan make:model TestTable
代码
use TestTable;
代码:
$data = TestTable::select('*')
->whereRaw("pttid NOT IN 5 OR (pttid = 6 AND is != 1)")
->whereBetween('sod', ["2019-01-01", "2019-01-01"])
->orWhereBetween('updat', ["2019-01-01", "2019-01-01"])
->get();
要过滤日期,请同时使用两种方法:whereBetween
和orWhereBetween
答案 2 :(得分:0)
我尝试过此方法,并且它可以工作。谢谢!