ThinkPHP5框架where實現or查詢的兩種方法


1.采用閉包方式 

tp5中采用閉包的方式:
$map['user_id']=1;
$map['status']=0;
$or_map['user_id']=$map['user_id'];
$or_map['audit']=['in',['1,2']];
$list = Db::name('table')->where(function ($query) use ($map) {
                    $query->where($map);
                })->whereOr(function ($query) use ($or_map) {
                    $query->where($or_map);
                })->select();
//生成的sql語句:
//SELECT * FROM `tp_table` WHERE  (  `user_id` = '1'  AND `status` = 0 ) OR (  `user_id` = '1'  AND `audit` IN ('1,2') )

2.普通方式

 
 $where = [
     'feed_uid'     => [ 'eq' , 5] ,
     'status' => [ [ 'eq' , 1] , [ 'eq' , 2 ] ,  [ 'eq' , 3 ] , 'or' ] ,
   ];
 $value = DealSpace::where($where)->count();
//最終的查詢條件為where feed_uid=5 and (status=1 or status =2 or status =3 )

轉載:https://blog.csdn.net/weixin_42330073/article/details/86496940

 

------------------------------------------------------------------------------------------------------------------------

            $where = ['fromid'=>$fromid,'toid'=>$toid];
            $whereOr = ['fromid'=>$toid,'toid'=>$fromid];

                $data = Db::name('workerman_communication')
                    ->where(function($query) use ($where){
                        $query->where($where);
                    })
                    ->whereOr(function($query) use ($whereOr){
                        $query->where($whereOr);
                    })
                    ->select();


            dump(Db::name('ht_workerman_communication')->getLastSql());die;
string(120) "SELECT * FROM `xq_workerman_communication` WHERE  (  `fromid` = 1  AND `toid` = 4 ) OR (  `fromid` = 4  AND `toid` = 1 )"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM