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