ThinkPHP Where 条件中使用表达式
本文转自:这里
Where 条件表达式格式为:
$map['字段名'] = array('表达式', '操作条件');
其中 $map 是一个普通的数组变量,可以根据自己需求而命名。上述格式中的表达式实际是运算符的意义:
| TP运算符 | SQL运算符 | 例子 | 实际查询条件 |
|---|---|---|---|
| eq | = | $map['id'] = array('eq',100); | 等效于:$map['id'] = 100; |
| neq | != | $map['id'] = array('neq',100); | id != 100 |
| gt | > | $map['id'] = array('gt',100); | id > 100 |
| egt | >= | $map['id'] = array('egt',100); | id >= 100 |
| lt | < | $map['id'] = array('lt',100); | id < 100 |
| elt | <= | $map['id'] = array('elt',100); | id <= 100 |
| like | like | $map<'username'> = array('like','Admin%'); | username like 'Admin%' |
| between | between and | $map['id'] = array('between','1,8'); | id BETWEEN 1 AND 8 |
| not between | not between and | $map['id'] = array('not between','1,8'); | id NOT BETWEEN 1 AND 8 |
| in | in | $map['id'] = array('in','1,5,8'); | id in(1,5,8) |
| not in | not in | $map['id'] = array('not in','1,5,8'); | id not in(1,5,8) |
| and(默认) | and | $map['id'] = array(array('gt',1),array('lt',10)); | (id > 1) AND (id < 10) |
| or | or | $map['id'] = array(array('gt',3),array('lt',10), 'or'); | (id > 3) OR (id < 10) |
| xor(异或) | xor | 两个输入中只有一个是true时,结果为true,否则为false,例子略。 | 1 xor 1 = 0 |
| exp | 综合表达式 | $map['id'] = array('exp','in(1,3,8)'); | $map['id'] = array('in','1,3,8'); |
补充说明
- 同 SQL 一样,ThinkPHP运算符不区分大小写,eq 与 EQ 一样。
- between、 in 条件支持字符串或者数组,即下面两种写法是等效的:
$map['id'] = array('not in','1,5,8');
$map['id'] = array('not in',array('1','5','8'));
exp 表达式
上表中的 exp 不是一个运算符,而是一个综合表达式以支持更复杂的条件设置。exp 的操作条件不会被当成字符串,可以使用任何 SQL 支持的语法,包括使用函数和字段名称。
exp 不仅用于 where 条件,也可以用于数据更新,如:
$Dao = M("Article");
// 构建 save 的数据数组,文章点击数+1
$data['id'] = 10;
$data['counter'] = array('exp','counter+1');
// 根据条件保存修改的数据
$User->save($data);
ThinkPHP Where 条件中使用表达式的更多相关文章
- (转)ThinkPHP Where 条件中使用表达式
转之--http://www.cnblogs.com/martin1009/archive/2012/08/24/2653718.html Where 条件表达式格式为: $map['字段名'] = ...
- Java8中Lambda表达式的10个例子
Java8中Lambda表达式的10个例子 例1 用Lambda表达式实现Runnable接口 //Before Java 8: new Thread(new Runnable() { @Overri ...
- SQLite中的表达式
SQLite中的表达式 在SELECT的基本完整形式中,我们会看到几乎是所有的子句都会使用到表达式.以下是SQLite支持的表达式类型. expr binary-op expr | ...
- 第86讲:Scala中For表达式的生成器、定义和过滤器
今天我们来看一下For表达式中的生成器,定义和过滤等内容. 让我们来看下代码 def main(args:Array[String]){ val lauren = Persons(" ...
- Thinkphp 查询条件 and 和 or同时使用即复合查询
thinkphp 3.2快捷查询OR查询&分割表示AND查询讲解 快捷查询方式是一种多字段查询的简化写法,可以进一步简化查询条件的写法,在多个字段之间用|分割表 ...
- C/C++ 语言中的表达式求值
在此,首先向裘老师致敬! 裘宗燕:C/C++ 语言中的表达式求值 经常可以在一些讨论组里看到下面的提问:“谁知道下面C语句给n赋什么值?” m = 1; n = m+++m++; 最近有位不相识的朋友 ...
- Kotlin中when表达式的使用:超强的switch(KAD 13)
作者:Antonio Leiva 时间:Feb 23, 2017 原文链接:https://antonioleiva.com/when-expression-kotlin/ 在Java(特别是Java ...
- JSONPath中的表达式
在JsonPath中使用表达式是一个非常好的功能,可以使用简洁和复杂的JsonPath.JsonPath中的表达式基本上是评估为布尔值的代码片段.基于结果,仅选择满足标准的节点.让我们看一下它的更多内 ...
- Python中lambda表达式的应用
lambda表达式 Python中定义了一个匿名函数叫做lambda表达式,个人理解实现的作用就是代替一些简单的函数,使得代码看上去更简洁并且可读性高.举个例子,我们有一个元组列表[(‘a’,1),( ...
随机推荐
- linux分区机制(MBR GPT)简介
- 微软本月将推Win10 望打破Win8所带来差评影响
系统妈 据香港“文汇报”7月15日报道,美国电脑巨擘微软13日宣布,新视窗系统10(Windows 10)将于7月29日推出.微软高层迈赫迪在网志表示,届时全球将有13个城市举办特别活动,表扬参与Wi ...
- C# 多线程之Task资料
博客 Stephen Toub From MicroSoft Crop. Stephen Cleary Parallelism in .NET 文章 It's All About the Sync ...
- Vmware虚拟机Devstack安装openstack(All in one)
Vmware虚拟机Devstack安装openstack(All in one) 博客园第一篇博客,先练习一下. 官方文档 环境 Vmware虚拟机 OS : Ubuntu 14.04 Nat网络,D ...
- SimpleDateFormat使用详解——日期、字符串应用
public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 求n!最后一位非零数
引子:求n!末尾0的个数 n!末尾的0来源只有2,5两个质数相乘.所以只需要考察n!中包含多少个2和多少个5.然后取其较小值即为所求.即ans=min(cnt(2),cnt(5)).而转念一想,cnt ...
- pImpl
之前看代码,一直对pIml这个用法一知半解,参考这里 的一篇文章后有所收获. 总结一下,pIml的好处如下: 第一,引入更多的头文件降低编译速度.而且这个声明当然写在一个头文件里,而头文件,是不能预编 ...