如何用查询构造器实现复杂的多条件查询?
如何用查询构造器实现复杂的多条件查询?
比如:
select * from table where a=1 and (b=2 or c=2) and (d=3 or e=3)
select * from table where a=1 and b=2 or c=2 and d=3 or e=3
特别是括号优先级的体现
比如:
select * from table where a=1 and (b=2 or c=2) and (d=3 or e=3)
select * from table where a=1 and b=2 or c=2 and d=3 or e=3
特别是括号优先级的体现
2 个回复
FiveSay - 成武
赞同来自: li53957105
Route::get('test', function () {
echo '<pre>';
dd(
'select * from table where a=1 and (b=2 or c=2) and (d=3 or e=3)',
DB::table('table')
->where('a', 1)
->where(function ($query) {
$query->where('b', 2)->orWhere('c', 2);
})
->where(function ($query) {
$query->where('d', 3)->orWhere('e', 3);
})
->toSql(),
'select * from table where a=1 and b=2 or c=2 and d=3 or e=3',
DB::table('table')
->where('a', 1)
->orWhere('b', 2)
->where('c', 2)
->orWhere('e', 3)
->toSql()
);
});
li53957105
赞同来自: