ThinkPHP6模型中多组条件逻辑或Or查询的使用

直接进入正题,如我们的查询条件是这样的:

(age > 20 and job_id = 3) or (sex = 1 and age < 20)

上面两组数据之间是 Or 的关系。

可使用两个闭包进行查询,代码如下:

php 复制代码
$map1 = [
            'age'  => 30,
            'sex'       => $id,
        ];
        $map2 = [
            'age'   => 1,
            'sex'     => $id,
        ];

        try{
            $lists= Orders::where(function($query) use($map1){
                $query->where($map1);
            })
                ->whereOr(function ($query) use ($map2){
                    $query->where($map2);
                })
                ->select();
        }catch (\Exception $e){
            echo $e->getMessage();
            return [];
        }

两份个条件组都要放到闭包里,否则会会变成: c1 and c2 or(c3 and c4);

相关推荐
weelinking1 天前
【产品】00_产品经理用Claude实现产品系列介绍
数据库·人工智能·sql·数据挖掘·github·产品经理
米高梅狮子1 天前
03.网络类服务实践
linux·运维·服务器·网络·kubernetes·centos·openstack
June`1 天前
网络编程时内核究竟做了什么???
linux·服务器·网络
原来是猿1 天前
腾讯云服务器端口开放完全指南
服务器·网络·腾讯云
2301_803934611 天前
Go语言如何做网络爬虫_Go语言爬虫开发教程【指南】
jvm·数据库·python
你的保护色1 天前
【无标题】
java·服务器·网络
楼兰公子1 天前
RK3588 + Linux7.0.3 网络工程调试错误速查手册
linux·网络·3588
Elnaij1 天前
Linux系统与系统编程(9)——自设计shell与基础IO
linux·服务器
秋91 天前
windows中安装redis
数据库·redis·缓存
IMPYLH1 天前
Linux 的 unexpand 命令
linux·运维·服务器·bash