环境:
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++]
938

被折叠的 条评论
为什么被折叠?



