where() public method

Sets the WHERE part of the query.

The method requires a $condition parameter, and optionally a $params parameter specifying the values to be bound to the query.

The $condition parameter should be either a string (e.g. 'id=1') or an array.

The $condition specified as an array can be in one of the following two formats:

  • hash format: ['column1' => value1, 'column2' => value2, ...]
  • operator format: [operator, operand1, operand2, ...]

A condition in hash format represents the following SQL expression in general: column1=value1 AND column2=value2 AND .... In case when a value is an array, an IN expression will be generated. And if a value is nullIS NULL will be used in the generated expression. Below are some examples:

  • ['type' => 1, 'status' => 2] generates (type = 1) AND (status = 2).
  • ['id' => [1, 2, 3], 'status' => 2] generates (id IN (1, 2, 3)) AND (status = 2).
  • ['status' => null] generates status IS NULL.

A condition in operator format generates the SQL expression according to the specified operator, which can be one of the followings:

  • and: the operands should be concatenated together using AND. For example, ['and', 'id=1', 'id=2'] will generate id=1 AND id=2. If an operand is an array, it will be converted into a string using the rules described here. For example, ['and', 'type=1', ['or', 'id=1', 'id=2']] will generate type=1 AND (id=1 OR id=2). The method will not do any quoting or escaping.

  • or: similar to the and operator except that the operands are concatenated using OR. For example, ['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]] will generate (type IN (7, 8, 9) OR (id IN (1, 2, 3))).

  • not: this will take only one operand and build the negation of it by prefixing the query string with NOT. For example ['not', ['attribute' => null]] will result in the condition NOT (attribute IS NULL).

  • between: operand 1 should be the column name, and operand 2 and 3 should be the starting and ending values of the range that the column is in. For example, ['between', 'id', 1, 10] will generate id BETWEEN 1 AND 10.

  • not between: similar to between except the BETWEEN is replaced with NOT BETWEEN in the generated condition.

  • in: operand 1 should be a column or DB expression, and operand 2 be an array representing the range of the values that the column or DB expression should be in. For example, ['in', 'id', [1, 2, 3]] will generate id IN (1, 2, 3). The method will properly quote the column name and escape values in the range.

    To create a composite IN condition you can use and array for the column name and value, where the values are indexed by the column name: ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']] ].

    You may also specify a sub-query that is used to get the values for the IN-condition: ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]

  • not in: similar to the in operator except that IN is replaced with NOT IN in the generated condition.

  • like: operand 1 should be a column or DB expression, and operand 2 be a string or an array representing the values that the column or DB expression should be like. For example, ['like', 'name', 'tester'] will generate name LIKE '%tester%'. When the value range is given as an array, multiple LIKE predicates will be generated and concatenated using AND. For example, ['like', 'name', ['test', 'sample']] will generate name LIKE '%test%' AND name LIKE '%sample%'. The method will properly quote the column name and escape special characters in the values. Sometimes, you may want to add the percentage characters to the matching value by yourself, you may supply a third operandfalse to do so. For example, ['like', 'name', '%tester', false] will generate name LIKE '%tester'.

  • or like: similar to the like operator except that OR is used to concatenate the LIKE predicates when operand 2 is an array.

  • not like: similar to the like operator except that LIKE is replaced with NOT LIKE in the generated condition.

  • or not like: similar to the not like operator except that OR is used to concatenate the NOT LIKE predicates.

  • exists: operand 1 is a query object that used to build an EXISTS condition. For example ['exists', (new Query())->select('id')->from('users')->where(['active' => 1])] will result in the following SQL expression: EXISTS (SELECT "id" FROM "users" WHERE "active"=1).

  • not exists: similar to the exists operator except that EXISTS is replaced with NOT EXISTS in the generated condition.

  • Additionally you can specify arbitrary operators as follows: A condition of ['>=', 'id', 10] will result in the following SQL expression: id >= 10.

where 常用条件范例的更多相关文章

  1. NSDate常用代码范例

    NSDate常用代码范例 NSDate类用于保存时间值,同时提供了一些方法来处理一些基于秒级别时差(Time Interval)运算和日期之间的早晚比较等. 1. 创建或初始化可用以下方法 用于创建N ...

  2. SQL常用条件操作符

    1.SQL的六类内容: (1)数据定义语言(DDL): 创建.删除表结构的语句,包括Create.Drop (2)数据控制语言(DCL): 为定义数据访问及修改权限而实现的语句,包括Grant.Rev ...

  3. 针对主流浏览器的CSS-HACK写法及IE常用条件注释

    一.通用区分方式: IE6.IE7能识别*,标准浏览器(如FF)不能识别*:IE6能识别*,但不能识别 !important:IE7能识别*,也能识别 !important:IE8能识别\0,不能识别 ...

  4. SQL语句 常用条件判断

    条件判断写法: 对每天记录执行操作时,判断所限制的条件-----> 操作符:                     =      <>(不匹配检查)       !=     &l ...

  5. Linux的iptables常用配置范例(1)

    以下是来自 http://wiki.ubuntu.org.cn/IptablesHowTo 上的配置说明 可以通过/sbin/iptables -F清除所有规则来暂时停止防火墙: (警告:这只适合在没 ...

  6. shell中的常用条件判断

    -e :该“文件名”是否存在.exit-d :该文件名是否为目录.dir-f  :该文件名是否为普通文件.file -b:该文件是否为块文件.block -r :该文件是否具有可读属性 read-w ...

  7. Linux的iptables常用配置范例(2)

    iptables -F   #清除所有规则 iptables -X  #清除所有自定义规则 iptables -Z   #各项计数归零 iptables -P INPUT DROP  #将input链 ...

  8. Linux的iptables常用配置范例(3)

    编辑/etc/rc.local,加入iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth1 -j MASQUERADE,外网口eth1为dhc ...

  9. IOS正则表达式 (身份证、电话、汉字等常用条件筛选)

    下面的正则列表   替换对应的正则规则 那个字符串就可以了  例如: //正则规则 NSString *regex = @"^((13[0-9])|(147)|(17[0-9])|(15[^ ...

随机推荐

  1. 爬虫(一)jupyter环境安装

    一.什么是Jupyter Notebook? 1. 简介 Jupyter Notebook是基于网页的用于交互计算的应用程序.其可被应用于全过程计算:开发.文档编写.运行代码和展示结果.——Jupyt ...

  2. Python----unittest discover()方法与执行顺序

    一.Unittest discover()可以根据不同的功能创建不同的测试文件,甚至是不同的测试目录,测试文件中还可以将不同的小功能划分为不同的测试类,在类下编写测试用例,让整体结构更加清晰一般是通过 ...

  3. cropper截图不压缩PHP上传裁剪后的图片

    cropperjs使用不多说网上都有很详细的介绍如下面: https://blog.csdn.net/lxy4239/article/details/78920979 主要讲下使用的经历 裁剪后图片不 ...

  4. mysql 插件相关命令

    # 查看mysql的插件 show plugins \G # 安装mysql 插件 INSTALL PLUGIN spartan SONAME 'ha_spartan.so'; # 卸载 UNINST ...

  5. VC调用外部程序

    #include <windows.h> int main() { STARTUPINFO mStatusInfo; memset(&mStatusInfo, 0, sizeof( ...

  6. Android 音视频深入 十六 FFmpeg 推流手机摄像头,实现直播 (附源码下载)

    源码地址https://github.com/979451341/RtmpCamera/tree/master 配置RMTP服务器,虽然之前说了,这里就直接粘贴过来吧 1.配置RTMP服务器 这个我不 ...

  7. 微信小程序 遇到的问题(新)

    1.调用wx.chooseImage(),调用系统相册,此时相册中的动图被转化成静态图,上传后也是静态图. 2.刚进微信小程序,onShow在安卓机下会调用两遍,iPhone下正常

  8. js中!和!!的区别及用法

    js中!的用法是比较灵活的,它除了做逻辑运算常常会用!做类型判断,可以用!与上对象来求得一个布尔值,1.!可将变量转换成boolean类型,null.undefined和空字符串取反都为false,其 ...

  9. 常用命令和sql

    常用命令: mvn idea:idea //生成.ipr项目文件 mvn clean install -Dmaven.test.skip=true mvn install:install-file - ...

  10. 活代码LINQ——07

    来源说明:https://blog.csdn.net/sha574810590/article/details/40738069 在LINQ中,数据源和查询结果实际上都是IEnumerable< ...