TP5框架如何用Db类查询这条sql语句

select * from goods where (id=1 and price=1) and (status=0 or status=2)
2025-05-02 01:38:11
推荐回答(1个)
回答1:

你这段sql,前面那个括号是没必要的,全部是and

Db::name('goods')->where('id',1)->where('price',1)
->whereIn('status',[0,2]);

这个写法可以达到你要的效果

也可以通过where嵌套,生成出来 就是你写的那种

Db::name('goods')->where('id',1)->where('price',1)
->where(function($db){
    return $db->where('status',1)->whereOr('status',2);
});

希望对你有帮