tp5 多条件where和TP5 Or查询的几种方法
时间:2021-11-10 作者:匿名
1.实测:
$id = Session::get('id'); $where = [ '域名d'=>[['eq',$id],['eq',0],'or'], '域名us'=>['eq',1] ]; $params = Db::table("ad_img")->alias('i') ->join('tp_user u','域名d=域名','left') ->field('域名e,域名name,域名,域名le,域名') ->where($where) ->select(false); $result = Db::field('域名us companyStatus,域名 recordId,域名name appName,域名name apkName,域名ver apkVer,域名size apkSize,域名it_time commitTime,域名lt,域名us recordStatus,域名us reportStatus,域名rt_num reportNum,域名us orderStatus') ->table(['t_record' => 'record']) ->join(['t_user' => 'user'], "域名 = 域名unt_id", 'left') ->join(['t_report' => 'report'], "域名 = 域名rd_id", 'left') ->join(['t_order' => 'order'], "域名_id = 域名unt_id and 域名rt_num = 域名rt_num", 'left') ->join(['t_company' => 'company'], "域名_id = 域名unt_id", 'left') ->where($type, $value) ->where($result, $status) ->where('域名us','<>', 2) ->where('域名unt_id', $userId) ->where('域名it_time', '>= time', $startTime) ->where('域名it_time', '<= time', $endTime) ->order('域名 desc') ->limit($fromId, $pageSize) ->select();
2.在TP3中想要or查询
条件可以为:
$condition['grade'] = 1; $condition['class'] = 3; $condition['sex'] = 2; $condtion['_logic'] = 'OR'; $list = M(‘user’)->where($condtion)->findall();
然后在TP5中尝试用where去这么查询发现一直在报错,查了手册之后发现TP5取消了_logic作为查询方式,而是新增了whereOr方法,下面是TP5中查询方式
域名
<?php namespace app\\index\\controller; use app\\index\\model\\UserModel; class User { public function index() { $condition['grade'] = 1; $condition['class'] = 3; $condition['sex'] = 2; $UserModel = new UserModel; $list = $UserModel->getlistwhereOr($condition); print_r($list); } }
域名
<?php namespace app\\index\\model; use app\\common\\model\\CommonModel; use think\\Db; use think\\Model; class UserModel extends CommonModel { public function __construct(){ parent::__construct(); } protected $name = 'User'; public function getlistwhereOr($condition) { $list =Db::name($this->name)->whereOr($condition)->select(); return $list; } }
执行域名 发现打印出来的数据就是我们要的筛选数据,
总结:TP5相比TP3中更新了很多我们经常用到的查询方式,而且写法更人性化,需要经常的去整理查看这些新方法
或者也可以用$where['custom_name|custom_phone'] = ['like', "%" . $post['keyword'] . '%'];这种方法或查询,如输入关键词模糊查找几个字段如下代码:
$where['land_no'] = 0; if ($post['keyword']) { $where['custom_name|custom_phone'] = ['like', "%" . $post['keyword'] . '%']; } $where['custom_id'] = 0; $num = db('land')->where($where)->select();
3.采用闭包方式
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') )
4.普通方式
$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 )