實測:
$id = Session::get('id');
$where = [
'i.tp_id'=>[['eq',$id],['eq',0],'or'],
'i.status'=>['eq',1]
];
$params = Db::table("ad_img")->alias('i')
->join('tp_user u','i.tp_id=u.id','left')
->field('i.image,u.username,u.id,u.mobile,u.ip')
->where($where)
->select(false);
$result = Db::field('company.status companyStatus,record.id recordId,record.app_name appName,record.apk_name apkName,record.apk_ver apkVer,record.apk_size apkSize,record.commit_time commitTime,record.result,record.status recordStatus,report.status reportStatus,report.report_num reportNum,order.status orderStatus')
->table(['t_record' => 'record'])
->join(['t_user' => 'user'], "user.id = record.account_id", 'left')
->join(['t_report' => 'report'], "record.id = report.record_id", 'left')
->join(['t_order' => 'order'], "order.user_id = record.account_id and order.report_num = report.report_num", 'left')
->join(['t_company' => 'company'], "company.user_id = record.account_id", 'left')
->where($type, $value)
->where($result, $status)
->where('company.status','<>', 2)
->where('record.account_id', $userId)
->where('record.commit_time', '>= time', $startTime)
->where('record.commit_time', '<= time', $endTime)
->order('record.id desc')
->limit($fromId, $pageSize)
->select();
原文鏈接:https://blog.csdn.net/qq_14969259/article/details/81585582
TP5 Or查詢的幾種方法
在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中查詢方式
User.php
<?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); } }
UserModel.php
<?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; } }
執行User.php 發現打印出來的數據就是我們要的篩選數據,
總結: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 )
-