where 常用条件范例
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 null
, IS 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]
generatesstatus 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 generateid=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 generatetype=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 usingOR
. 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 conditionNOT (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 generateid BETWEEN 1 AND 10
.not between: similar to
between
except theBETWEEN
is replaced withNOT 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 generateid 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 thatIN
is replaced withNOT 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 generatename LIKE '%tester%'
. When the value range is given as an array, multipleLIKE
predicates will be generated and concatenated usingAND
. For example,['like', 'name', ['test', 'sample']]
will generatename 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 generatename LIKE '%tester'
.or like: similar to the
like
operator except thatOR
is used to concatenate theLIKE
predicates when operand 2 is an array.not like: similar to the
like
operator except thatLIKE
is replaced withNOT LIKE
in the generated condition.or not like: similar to the
not like
operator except thatOR
is used to concatenate theNOT 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 thatEXISTS
is replaced withNOT 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 常用条件范例的更多相关文章
- NSDate常用代码范例
NSDate常用代码范例 NSDate类用于保存时间值,同时提供了一些方法来处理一些基于秒级别时差(Time Interval)运算和日期之间的早晚比较等. 1. 创建或初始化可用以下方法 用于创建N ...
- SQL常用条件操作符
1.SQL的六类内容: (1)数据定义语言(DDL): 创建.删除表结构的语句,包括Create.Drop (2)数据控制语言(DCL): 为定义数据访问及修改权限而实现的语句,包括Grant.Rev ...
- 针对主流浏览器的CSS-HACK写法及IE常用条件注释
一.通用区分方式: IE6.IE7能识别*,标准浏览器(如FF)不能识别*:IE6能识别*,但不能识别 !important:IE7能识别*,也能识别 !important:IE8能识别\0,不能识别 ...
- SQL语句 常用条件判断
条件判断写法: 对每天记录执行操作时,判断所限制的条件-----> 操作符: = <>(不匹配检查) != &l ...
- Linux的iptables常用配置范例(1)
以下是来自 http://wiki.ubuntu.org.cn/IptablesHowTo 上的配置说明 可以通过/sbin/iptables -F清除所有规则来暂时停止防火墙: (警告:这只适合在没 ...
- shell中的常用条件判断
-e :该“文件名”是否存在.exit-d :该文件名是否为目录.dir-f :该文件名是否为普通文件.file -b:该文件是否为块文件.block -r :该文件是否具有可读属性 read-w ...
- Linux的iptables常用配置范例(2)
iptables -F #清除所有规则 iptables -X #清除所有自定义规则 iptables -Z #各项计数归零 iptables -P INPUT DROP #将input链 ...
- Linux的iptables常用配置范例(3)
编辑/etc/rc.local,加入iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth1 -j MASQUERADE,外网口eth1为dhc ...
- IOS正则表达式 (身份证、电话、汉字等常用条件筛选)
下面的正则列表 替换对应的正则规则 那个字符串就可以了 例如: //正则规则 NSString *regex = @"^((13[0-9])|(147)|(17[0-9])|(15[^ ...
随机推荐
- win10释放的wifi热点手机连不上
直入正题吧…… 在win10的搜索框里输入ser,找到“windows服务”选项,点击进入,如下图 找到下图所示的两个服务,然后,右键,属性,启动类型改为自动,然后点确定,确定完以后,再右键,点启动, ...
- 记一次tomcat运行起来了但是项目没起来的问题
解决办法是: 先是tomcat的conf文件夹下的servel.xml中这两个值改成false. 然后重新运行maven的package打包,再运行项目就行了.
- 维修数列 Splay(这可能是我写过最麻烦的题之一了。。。用平衡树维护dp。。。丧心病狂啊。。。。)
题目来源BZOJ1500 这题的思路: 1.这题的话,稍微会splay的人,一般前面四个都不是问题..主要是最后的一个,要你在修改的同时要维护好最大字段和... 2.最大字段和其实就是区间合并.具体操 ...
- java 编译
package javacodeforstudy.testcode; public class Helloworld{ public static void main(String[] args) { ...
- css属性应用bug大杂烩(后续继续更新)
一.Flex布局使用时的坑: 1.常见的左右分布的flex布局中,左侧给定宽度,右侧占满剩余空间,但当右侧中文字内容很多时,会挤占左侧空间,时左侧不能按照定宽显示. <style> .fa ...
- China MVP Community Connection 2017
在微软北京和小朋友们参加编程一小时活动
- 跟随我在oracle学习php(19)
Order by子句 形式: order by 排序字段1 [排序方式], 排序字段2 [排序方式], ..... 说明: 对前面取得的数据(含from子句,where子句,group子句, ...
- 数据类型、运算符及Scanner类练习
数字加密.要求输入一个四位的正整数,每位数字加5再除以10取余,并替换该数字,再千位数与个位数互换,十位数与百位数互换. import java.util.Scanner;/** * 加密数字问题 * ...
- 爬取字段 spider_text
__author__ = 'sus'import urllibimport urllib2import re def getPage(url): #获取网页 request = urll ...
- typescript 属性默认值使用箭头函数 this指向问题
今天注意到前端小伙伴用react 定义component class的方法的时候是通过箭头函数的方式,表示好奇. class Test extends React.Component { public ...