YII2 model where 条件拼接
- 熟悉Yii2的查询条件后,用Active Record查询数据非常方便。
- 以下我们介绍where()方法当中,条件的拼装方式。
- #某个值为null,会用IS NULL来生成语句:
- ['type' => 1, 'status' => 2] // 生成:(type = 1) AND (status = 2)
- ['id' => [1, 2, 3], 'status' => 2] // 生成:(id IN (1, 2, 3)) AND (status = 2)
- ['status' => null] // 生成:status IS NULL
- ['NOT', ['type' => null]] // 生成:type IS NOT NULL
- #对比
- ['>', 'id', 1] // 生成:id > 1
- ['<', 'id', 100] // 生成:id < 100
- ['=', 'id', 10] // 生成:id = 10
- ['>=', 'id', 1] // 生成:id >= 1
- ['<=', 'id', 100] // 生成:id <= 100
- ['!=', 'id', 10] // 生成:id != 10
- ['and', 'id' => 1, 'id' => 2] // 生成:id=1 AND id=2
- ['and', 'id=1', 'id=2'] // 生成:id=1 AND id=2
- ['and', 'type=1', ['or', 'id=1', 'id=2']] // 生成:type=1 AND (id=1 OR id=2)
- ['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]] // 生成:(type IN (7, 8, 9) OR (id IN (1, 2, 3)))
- ['not', ['attribute' => null]] // 生成:NOT (attribute IS NULL)
- ['between', 'id', 1, 10] // 生成:id BETWEEN 1 AND 10
- ['not between', 'id', 1, 10] // 生成:id NOT BETWEEN 1 AND 10
- ['in', 'id', [1, 2, 3]] // 生成:id IN (1, 2, 3)
- ['id' => [4, 8, 15]] // 生成:id IN (4, 8, 15)
- ['not in', 'id', [1, 2, 3]] // 生成:id NOT IN (1, 2, 3)
- ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]] // 生成:(`id`, `name`) IN ((1, 'foo'), (2, 'bar'))
- #用子查询作为IN条件的值,如下:
- ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]
- ['like', 'name', 'tester'] // 生成:name LIKE '%tester%'
- ['like', 'name', ['test', 'sample']] // 生成:name LIKE '%test%' AND name LIKE '%sample%'
- ['like', 'name', '%tester', false] // 生成:name LIKE '%tester'
- // 这是自定义查询方式,要传入值为false的运算数3,并且自行添加%
- ['or like', 'name', ['test', 'sample']] // 生成:name LIKE '%test%' OR name LIKE '%sample%'
- ['not like', 'name', 'tester'] // 生成:name NOT LIKE '%tester%'
- ['or not like', 'name', ['test', 'sample']] // 生成:name NOT LIKE '%test%' OR name NOT LIKE '%sample%'
- ['exists', (new Query())->select('id')->from('users')->where(['active' => 1])] // 生成:EXISTS (SELECT "id" FROM "users" WHERE "active"=1)
YII2 model where 条件拼接的更多相关文章
- MongoDB学习笔记~复杂条件拼接和正则的使用
在大叔lind框架里有日志组件logger,而在日志实现方式上有file,mongodb,sql,json等方式,对分布式日志处理上大叔推荐使用mongodb进行存储,除了它的高效写入,灵活的结构外, ...
- springboot~mogodb多条件拼接
起因 当前我们使用mongodb进行查询时,有时我们的条件是分块生成的,它可能来自一个列表里,我们的条件需要根据列表去返回数据,这里有个问题,如果遍历列表,然后每次都去从mongodb里查询数据 ,这 ...
- 理想中的SQL语句条件拼接方式 (二)
问题以及想要的效果,不重复叙述,如果需要的请先看 理想中的SQL语句条件拼接方式 . 效果 现在有2个类映射数据库的2张表,结构如下: public class User { public int U ...
- Laravel where条件拼接,数组拼接where条件
问题描述:laravel where 条件拼接 Like出错,搜索不到要搜索的内容. 问题代码: // 作物 $crop_class_id = $request->crop_class_id; ...
- django ORM model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct
版权归作者所有,任何形式转载请联系作者.作者:petanne(来自豆瓣)来源:https://www.douban.com/note/301166150/ 1.多表连接查询:感觉django太NX了. ...
- Entity Framework解决sql 条件拼接,完美解决 解决 不支持 LINQ 表达式节点类型“Invoke”【转】
传统的操作数据库方式,筛选数据需要用StringBuilder拼接一大堆的WHERE子句. 在Entity Framework中,代码稍有不慎就会造成巨大性能消耗,如: using(var db=ne ...
- Yii2 Model的一些常用rules规则,使用Validator验证
1. Yii2里 model在使用load方法加载浏览器的值的时候,会进行rules验证.这时候可以使用场景,让model对不同场景使用不同验证方式 2. 可以用attributeLabels()来指 ...
- yii2 中where条件查询
在Yii的Model里进行查询的时候 where是必不可少的. Where方法声明为 static where( $condition ) 其中参数 $condition 类型为字符串或者数组1.字符 ...
- python django model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct[转]
1.多表连接查询:当我知道这点的时候顿时觉得django太NX了. class A(models.Model): name = models.CharField(u'名称') clas ...
随机推荐
- Git生成SSH密钥
git config --global user.name "yangjianliang"配置用户名 git config --global user.email "52 ...
- 优先队列(堆) -数据结构(C语言实现)
数据结构与算法分析 优先队列 模型 Insert(插入) == Enqueue(入队) DeleteMin(删除最小者) == Dequeue(出队) 基本实现 简单链表:在表头插入,并遍历该链表以删 ...
- 使用Idea工具创建Maven WebApp项目
(1)New Project,选择模板,配置SDK (2)配置项目名及项目组名 GroupID是项目组织唯一的标识符, 比如我的项目叫test001 那么GroupID应该是 com.lixiaomi ...
- 洛谷【P1052】过河
https://www.luogu.org/problemnew/show/P1052 题目描述 在河上有一座长度为 L 的独木桥, 一只青蛙想沿着独木桥从河的一侧跳到另一侧. 在桥上有一些石子, 青 ...
- mysql把一字段拆分为多行
sql语句 select a.house_no as '房子',substring_index(substring_index(a.name,',',b.help_topic_id+1),',',-1 ...
- React Native 【学习总结】-【常用命令】
前言 刚接触RN,相信很多人无从下手,不知道下一步要干什么,能干什么,本次学习围绕这个问题,将RN的常用命令总结一下,帮助你快速上手 架构理解 光知道命令的作用,远远不够,如果知道命令背后的意义,才能 ...
- 3.Airflow使用
1. airflow简介2. 相关概念2.1 服务进程2.1.1. web server2.1.2. scheduler2.1.3. worker2.1.4. celery flower2.2 相关概 ...
- Walking Between Houses(贪心+思维)
Walking Between Houses There are nn houses in a row. They are numbered from 11 to nn in order from l ...
- FIsherman丶Team
小组成员:郝恒杰,洪佳兴,张子祥 组长:郝恒杰 项目:Fisher Job(渔夫兼职) 简介: 我们的产品渔夫兼职是为了解决大学生兼职群体 的痛苦,他们需要一个好的渠道去找一个让自己满意的兼职,但是现 ...
- Python:元组操作总结
Python的元组和列表类似,不同之处在于元组中的元素不能修改(因此元组又称为只读列表),且元组使用小括号而列表使用中括号,如下: tup1=('physics','chemistry',1997,2 ...