zoukankan      html  css  js  c++  java
  • 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],   
      ];
  • 相关阅读:
    ASP.NET 概述
    用vs调试项目页面无样式
    eniac世界第二台计算机
    汇编语言
    操作系统发展史
    网站架构发展
    简单分布式系统构建知识
    Android常用adb命令
    USB 3.0规范中译本 第4章 超高速数据流模型
    ECMAScript 6 &ECMAScript 5(在线手册)
  • 原文地址:https://www.cnblogs.com/bneglect/p/12090412.html
Copyright © 2011-2022 走看看