您现在的位置是:主页 > news > 长沙网站建设设计/企业网站设计服务
长沙网站建设设计/企业网站设计服务
admin2025/6/15 4:35:14【news】
简介长沙网站建设设计,企业网站设计服务,政府网站建设需求,太原网站搜索排名在TP3中想要or查询条件可以为:$condition[grade] 1;$condition[class] 3;$condition[sex] 2;$condtion[_logic] OR;$list M(‘user’)->where($condtion)->findall();然后在TP5中尝试用where去这么查询发现一直在报错,查了手册之后发现TP5取消…
在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
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
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 )
这就是微学网-程序员之家为你提供的"TP5 Or查询的几种方法"希望对你有所帮助.本文来自网络,转载请注明出处:http://www.weixuecn.cn/article/14064.html