thinkphp 多条件复杂组合查询(_complex, _logic)

环境:

thinkphp3.2

需求:

想要生成这个
(where条件1 or where条件2) and (where条件3 or where条件4)
比较复杂的 组合查询:想要生成的sql:

	SELECT *
	FROM aaa
	WHERE ( 
	    ( ( member.real_name LIKE '%朱%' ) OR ( `minister_file_no` = '朱' ) ) and
	     ( ( member2.real_name LIKE '%20396%' ) OR ( `nq_file_no` = '20396' ) ) 
	)

解决:

这样拼接 where条件:

        $whereIdx = 0;
        //  第一个or所在的括号内容
        if ($leaderInfo != '') {
            $where_group1['member.real_name'] = ['like', "%$leaderInfo%"];
            $where_group1['minister_file_no'] = ['eq', "$leaderInfo"];
            $where_group1['_logic'] = 'or';
            $map['_complex'][$whereIdx++] = $where_group1;
        }

        //  第二个or所在的括号内容
        if ($nq_info != '') {
            $where_group2['member2.real_name'] = ['like', "%$nq_info%"];
            $where_group2['nq_file_no'] = ['eq', "$nq_info"];
            $where_group2['_logic'] = 'or';
            $map['_complex'][$whereIdx++] = $where_group2;
        }
        M('模型')->where($map)->select();

关键:

重点是
$map['_complex'][$whereIdx++]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值