1. 熟悉Yii2的查询条件后,用Active Record查询数据非常方便。
  2. 以下我们介绍where()方法当中,条件的拼装方式。
  3. #某个值为null,会用IS NULL来生成语句:
  4. ['type' => 1, 'status' => 2]            // 生成:(type = 1) AND (status = 2)
  5. ['id' => [1, 2, 3], 'status' => 2]      // 生成:(id IN (1, 2, 3)) AND (status = 2)
  6. ['status' => null]                      // 生成:status IS NULL
  7. ['NOT', ['type' => null]]                // 生成:type IS NOT NULL
  8. #对比
  9. ['>', 'id', 1]                                        // 生成:id > 1
  10. ['<', 'id', 100]                                      // 生成:id < 100
  11. ['=', 'id', 10]                                       // 生成:id = 10
  12. ['>=', 'id', 1]                                       // 生成:id >= 1
  13. ['<=', 'id', 100]                                     // 生成:id <= 100
  14. ['!=', 'id', 10]                                      // 生成:id != 10
  15. ['and', 'id' => 1, 'id' => 2]                        // 生成:id=1 AND id=2
  16. ['and', 'id=1', 'id=2']                              // 生成:id=1 AND id=2
  17. ['and', 'type=1', ['or', 'id=1', 'id=2']]            // 生成:type=1 AND (id=1 OR id=2)
  18. ['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]]   // 生成:(type IN (7, 8, 9) OR (id IN (1, 2, 3)))
  19. ['not', ['attribute' => null]]                       // 生成:NOT (attribute IS NULL)
  20. ['between', 'id', 1, 10]                             // 生成:id BETWEEN 1 AND 10
  21. ['not between', 'id', 1, 10]                         // 生成:id NOT BETWEEN 1 AND 10
  22. ['in', 'id', [1, 2, 3]]                               // 生成:id IN (1, 2, 3)
  23. ['id' => [4, 8, 15]]                               // 生成:id IN (4, 8, 15)
  24. ['not in', 'id', [1, 2, 3]]                           // 生成:id NOT IN (1, 2, 3)
  25. ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]]  // 生成:(`id`, `name`) IN ((1, 'foo'), (2, 'bar'))
  26. #用子查询作为IN条件的值,如下:
  27. ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]
  28. ['like', 'name', 'tester']                             // 生成:name LIKE '%tester%'
  29. ['like', 'name', ['test', 'sample']]                   // 生成:name LIKE '%test%' AND name LIKE '%sample%'
  30. ['like', 'name', '%tester', false]                     // 生成:name LIKE '%tester'
  31. // 这是自定义查询方式,要传入值为false的运算数3,并且自行添加%
  32. ['or like', 'name', ['test', 'sample']]                 // 生成:name LIKE '%test%' OR name LIKE '%sample%'
  33. ['not like', 'name', 'tester']                          // 生成:name NOT LIKE '%tester%'
  34. ['or not like', 'name', ['test', 'sample']]             // 生成:name NOT LIKE '%test%' OR name NOT LIKE '%sample%'
  35. ['exists', (new Query())->select('id')->from('users')->where(['active' => 1])] // 生成:EXISTS (SELECT "id" FROM "users" WHERE "active"=1)

YII2 model where 条件拼接的更多相关文章

  1. MongoDB学习笔记~复杂条件拼接和正则的使用

    在大叔lind框架里有日志组件logger,而在日志实现方式上有file,mongodb,sql,json等方式,对分布式日志处理上大叔推荐使用mongodb进行存储,除了它的高效写入,灵活的结构外, ...

  2. springboot~mogodb多条件拼接

    起因 当前我们使用mongodb进行查询时,有时我们的条件是分块生成的,它可能来自一个列表里,我们的条件需要根据列表去返回数据,这里有个问题,如果遍历列表,然后每次都去从mongodb里查询数据 ,这 ...

  3. 理想中的SQL语句条件拼接方式 (二)

    问题以及想要的效果,不重复叙述,如果需要的请先看 理想中的SQL语句条件拼接方式 . 效果 现在有2个类映射数据库的2张表,结构如下: public class User { public int U ...

  4. Laravel where条件拼接,数组拼接where条件

    问题描述:laravel where 条件拼接 Like出错,搜索不到要搜索的内容. 问题代码: // 作物 $crop_class_id = $request->crop_class_id; ...

  5. django ORM model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct

    版权归作者所有,任何形式转载请联系作者.作者:petanne(来自豆瓣)来源:https://www.douban.com/note/301166150/ 1.多表连接查询:感觉django太NX了. ...

  6. Entity Framework解决sql 条件拼接,完美解决 解决 不支持 LINQ 表达式节点类型“Invoke”【转】

    传统的操作数据库方式,筛选数据需要用StringBuilder拼接一大堆的WHERE子句. 在Entity Framework中,代码稍有不慎就会造成巨大性能消耗,如: using(var db=ne ...

  7. Yii2 Model的一些常用rules规则,使用Validator验证

    1. Yii2里 model在使用load方法加载浏览器的值的时候,会进行rules验证.这时候可以使用场景,让model对不同场景使用不同验证方式 2. 可以用attributeLabels()来指 ...

  8. yii2 中where条件查询

    在Yii的Model里进行查询的时候 where是必不可少的. Where方法声明为 static where( $condition ) 其中参数 $condition 类型为字符串或者数组1.字符 ...

  9. python django model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct[转]

    1.多表连接查询:当我知道这点的时候顿时觉得django太NX了.   class A(models.Model):     name = models.CharField(u'名称')   clas ...

随机推荐

  1. Git生成SSH密钥

    git config --global user.name "yangjianliang"配置用户名 git config --global user.email "52 ...

  2. 优先队列(堆) -数据结构(C语言实现)

    数据结构与算法分析 优先队列 模型 Insert(插入) == Enqueue(入队) DeleteMin(删除最小者) == Dequeue(出队) 基本实现 简单链表:在表头插入,并遍历该链表以删 ...

  3. 使用Idea工具创建Maven WebApp项目

    (1)New Project,选择模板,配置SDK (2)配置项目名及项目组名 GroupID是项目组织唯一的标识符, 比如我的项目叫test001 那么GroupID应该是 com.lixiaomi ...

  4. 洛谷【P1052】过河

    https://www.luogu.org/problemnew/show/P1052 题目描述 在河上有一座长度为 L 的独木桥, 一只青蛙想沿着独木桥从河的一侧跳到另一侧. 在桥上有一些石子, 青 ...

  5. mysql把一字段拆分为多行

    sql语句 select a.house_no as '房子',substring_index(substring_index(a.name,',',b.help_topic_id+1),',',-1 ...

  6. React Native 【学习总结】-【常用命令】

    前言 刚接触RN,相信很多人无从下手,不知道下一步要干什么,能干什么,本次学习围绕这个问题,将RN的常用命令总结一下,帮助你快速上手 架构理解 光知道命令的作用,远远不够,如果知道命令背后的意义,才能 ...

  7. 3.Airflow使用

    1. airflow简介2. 相关概念2.1 服务进程2.1.1. web server2.1.2. scheduler2.1.3. worker2.1.4. celery flower2.2 相关概 ...

  8. Walking Between Houses(贪心+思维)

    Walking Between Houses There are nn houses in a row. They are numbered from 11 to nn in order from l ...

  9. FIsherman丶Team

    小组成员:郝恒杰,洪佳兴,张子祥 组长:郝恒杰 项目:Fisher Job(渔夫兼职) 简介: 我们的产品渔夫兼职是为了解决大学生兼职群体 的痛苦,他们需要一个好的渠道去找一个让自己满意的兼职,但是现 ...

  10. Python:元组操作总结

    Python的元组和列表类似,不同之处在于元组中的元素不能修改(因此元组又称为只读列表),且元组使用小括号而列表使用中括号,如下: tup1=('physics','chemistry',1997,2 ...