• yii where 条件的使用


    • 当yii查询的条件变多的时候,where的写法会变化,比如查询分数在0,30之间,并且is_delete=0,id=$id的。

      如果像下面这样写,就达不到想要的结果

      //该方法不可用
      $where
      = []; $where['desc_id'] = $id; $where['is_delete'] = 0; //下面的$where 数组重新赋值了,就会把上面的where条件覆盖掉。条件变少了,查询结果会变化 $where = [ 'and', ['<=', 'minscore', $score], ['>=', 'maxscore', $score], ];

      这个时候可能会想到改变一下顺序是否能够达到目的呢?比如下面这样:

      //该方法不可用
      $where
      = [ 'and', ['<=', 'minscore', $score], ['>=', 'maxscore', $score], //达不到目的,舍弃 ]; $where['desc_id'] = $id; $where['is_delete'] = 0;

      经测试发现,这种根本不行,那只能换一种实现方式了。使用下面的方法

      //andWhere  或者 orWhere
      $where = [
            'and',
            ['<=', 'minscore', $score],
            ['>=', 'maxscore', $score],
      ];
      $andwhere['desc_id'] = $id;
      $andwhere['is_delete'] = 0;
      $assessment = PsychologicalAssessment::find()
                  ->select('id, title, content')
                  ->where($where)
                  ->andWhere($andwhere)
                  ->one();
    • yii like  单边加%,目的是使用索引
      $where = [
        'and',
        ['like', 'name', $name.'%', false],   
      ];
  • 相关阅读:
    tableView.contentInset
    Xcode 显示行数
    翻译技巧2
    程序员的自我修养学习笔记——第一章
    C++11 正则表达式——基础知识介绍
    C++11 正则表达式——实例1
    C++11 多线程
    程序员的自我修养——说明
    C++11 生产者消费者
    C++11 正则表达式——实例3
  • 原文地址:https://www.cnblogs.com/bneglect/p/12090412.html
Copyright © 2020-2023  润新知