thinkphp _complex 复合查询 where多个子组实现

SELECT * FROM `user` WHERE (  `mobile` = '13824653465' OR `nickname` = 'evan' OR `openid` = '14545-fdes'  ) AND (  `bind_seller` = 'fdaflj24214'  OR `seller_type` = '1'  ) AND ( `reg_time` < 12324568  AND  `login_count` > 10  )

  THINKPHP 数组where实现多个复合查询,例如上面的sql。查询官方手册,感觉无法实现。于是想到修改一下THINKPHP框架的DB类。

thinkphp 3.1 文件所在 \ThinkPHP\Lib\Core\Db.class.php

tthinkphp 3.2  \ThinkPHP\Library\Think\Db\Driver.class.php

修改这个方法

 1     protected function parseThinkWhere($key,$val) {
 2         $whereStr   = '';
 3         switch($key) {
 4             case '_string':
 5                 // 字符串模式查询条件
 6                 $whereStr = $val;
 7                 break;
 8             case '_complex':
 9             case '_complex1': //增加多个case分支,支持多个符合 只查询
10             case '_complex2':
11             case '_complex3':
12             case '_complex4':13                 // 复合查询条件
14                 $whereStr = substr($this->parseWhere($val),6);
15                 break;
16             case '_query':
17                 // 字符串模式查询条件
18                 parse_str($val,$where);
19                 if(isset($where['_logic'])) {
20                     $op   =  ' '.strtoupper($where['_logic']).' ';
21                     unset($where['_logic']);
22                 }else{
23                     $op   =  ' AND ';
24                 }
25                 $array   =  array();
26                 foreach ($where as $field=>$data)
27                     $array[] = $this->parseKey($field).' = '.$this->parseValue($data);
28                 $whereStr   = implode($op,$array);
29                 break;
30         }
31         return $whereStr;
32     }

 

 

 

 $      where['mobile'] = '13824653465';
        $where['_logic']  = 'or';
        $where['nickname'] = 'evan';
        $where['openid']  = '14545-fdes';



        $where2['bind_seller'] = 'fdaflj24214';
        $where2['seller_type'] = '1';
        $where2['_logic']      = 'or';

        $where3['reg_time']    = array('lt',12324568);
        $where3['login_count'] = array('gt',10);

        $map['_complex'] = $where;
        $map['_complex1']= $where2;
        $map['_complex2']= $where3;

        $sql = M('User')->where($map)->select();
        echo M()->getLastSql();

  

转载于:https://www.cnblogs.com/guohong-hu/p/8005951.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值