tp5 多条件where和TP5 Or查询的几种方法


实测:

$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查询的几种方法

发布时间:2020-11-14 来源:未知 点击:1761 次
 
 

在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.采用闭包方式 

  1. tp5中采用闭包的方式:
  2.  
    $map[ 'user_id']=1;
  3.  
    $map[ 'status']=0;
  4.  
    $or_map[ 'user_id']=$map['user_id'];
  5.  
    $or_map[ 'audit']=['in',['1,2']];
  6.  
    $list = Db::name( 'table')->where(function ($query) use ($map) {
  7.  
                        $query->where($map);
  8.  
                    })->whereOr( function ($query) use ($or_map) {
  9.  
                        $query->where($or_map);
  10.  
                    })->select();
  11.  
    //生成的sql语句:
  12.  
    //SELECT * FROM `tp_table` WHERE  (  `user_id` = '1'  AND `status` = 0 ) OR (  `user_id` = '1'  AND `audit` IN ('1,2') )

 

4.普通方式

  1. $where = [
  2.  
    'feed_uid' => [ 'eq' , 5] ,
  3.  
    'status' => [ [ 'eq' , 1] , [ 'eq' , 2 ] , [ 'eq' , 3 ] , 'or' ] ,
  4.  
    ];
  5.  
    $value = DealSpace::where($where)->count();
  6.  
    //最终的查询条件为where feed_uid=5 and (status=1 or status =2 or status =3 )
  7.  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM