Yii2的where方法使用大全

PHP工程师说白了也是CURD工程师,所做的工作无非是各种业务的CURD,掌握各种where的使用,MM再也不用担心我的程式会被攻击了~

Yii中,使用where方法是非常频繁的,而且where方法本身的使用技巧比较的多,在这里我梳理了一些常用的,以便于以后工作中的随时查阅的需要,也希望能帮助到查阅此文的你,也欢迎各位的补充,不足之处希望指出!

还是从简入深吧!ps:这里的数据表是进行模拟的。

1)简单的赋值

[php]  view plain  copy
  1. ->where("id=:id", [  
  2.     'id' => 1  
  3. ])  

上面的程式等同于 id=1


2)AND查询

[php]  view plain  copy
  1. ->where("id=:id and pack_name=:pack_name", [  
  2.     ':id' => 1,  
  3.     ':pack_name' => 'com.famigo.sandbox'  
  4. ])  

或者

[php]  view plain  copy
  1. ->where([  
  2.     'and',  
  3.     'id=:id',  
  4.     'pack_name=:pack_name'  
  5. ], [  
  6.     ':id' => 1,  
  7.     ':pack_name' => 'com.famigo.sandbox'  
  8. ])  
这两种程式等同于 id=1 AND pack_name='com.famigo.sandbox'

3)OR查询

[php]  view plain  copy
  1. ->where("id=:id or pack_name=:pack_name", [  
  2.     ':id' => 1,  
  3.     ':pack_name' => 'com.famigo.sandbox'  
  4. ])  
或者

[php]  view plain  copy
  1. ->where([  
  2.     'or',  
  3.     'id=:id',  
  4.     'pack_name=:pack_name'  
  5. ], [  
  6.     ':id' => 1,  
  7.     ':pack_name' => 'com.famigo.sandbox'  
  8. ])  
这两种程式等同于 id=1 OR pack_name='com.famigo.sandbox'

4)AND OR 混合查询

[php]  view plain  copy
  1. ->where([  
  2.     'and',  
  3.     'display=:display',  
  4.     [  
  5.         'or',  
  6.         'id=:id1',  
  7.         'id=:id2'  
  8.     ]  
  9. ], [  
  10.     ':display' => 1,  
  11.     ':id1' => 1,  
  12.     ':id2' => 2  
  13. ])  
上面的程式等同于(display=1) AND ((id=1) OR (id=2))

5)IN查询

[php]  view plain  copy
  1. ->where([  
  2.     'in''id', [1, 3, 5, 6]  
  3. ])  
上面程式等同于 id in (1, 3, 5, 6)

[php]  view plain  copy
  1. ->where([  
  2.     'and',  
  3.     'display=:display',  
  4.     'lang=:lang',  
  5.     [  
  6.     'in''id', [1, 3, 5, 6]  
  7.     ]  
  8. ], [  
  9.     ':display' => 1,  
  10.     ':lang' => 2  
  11. ])  
上面程式等同于 (display=1) AND (lang=2) AND (`id` IN (1, 3, 5, 6))

更为麻烦点的例子

[php]  view plain  copy
  1. ->where([  
  2.     'or',  
  3.     [  
  4.         'and',  
  5.         'display=:display1',  
  6.         [  
  7.         'in''id', [1, 3, 5, 6]  
  8.         ]  
  9.     ],  
  10.     [  
  11.         'and',  
  12.         'display=:display2',  
  13.         [  
  14.         'in''id', [2, 4, 8, 9]  
  15.         ]  
  16.     ]  
  17. ], [  
  18.     ':display1' => 1,  
  19.     ':display2' => 2,  
  20. ])  
上面程式等同于((display=1) AND (`id` IN (1, 3, 5, 6))) OR ((display=2) AND (`id` IN (2, 4, 8, 9)))

6)NOT IN 查询

[php]  view plain  copy
  1. ->where([  
  2.     'not in''id', [1, 2, 4, 3]  
  3. ])  
上面程式等同于`id` NOT IN (1, 2, 4, 3)

复杂的使用方法和上述的IN是一样的,参考即可。

7)LIKE 查询

[php]  view plain  copy
  1. ->where([  
  2.     'like''pack_name''%sandbox%'  
  3. ])  
上面程式等同于`pack_name` LIKE '%sandbox%'

[php]  view plain  copy
  1. ->where([  
  2.     'like''pack_name', [  
  3.         '%sandbox%',  
  4.         'com.famigo%'  
  5.     ]  
  6. ])  

上面程式等同于`pack_name` LIKE '%sandbox%' AND `pack_name` LIKE 'com.famigo%'

[php]  view plain  copy
  1. ->where([  
  2.     'or like''pack_name', [  
  3.         '%sandbox%',  
  4.         'com.famigo%'  
  5.     ]  
  6. ])  
上面程式等同于`pack_name` LIKE '%sandbox%' OR `pack_name` LIKE 'com.famigo%'

[php]  view plain  copy
  1. ->where([  
  2.     'or not like''pack_name', [  
  3.         '%sandbox%',  
  4.         'com.famigo%'  
  5.     ]  
  6. ])  
上面程式等同于`pack_name` NOT LIKE '%sandbox%' OR `pack_name` NOT LIKE 'com.famigo%'

[php]  view plain  copy
  1. ->where([  
  2.     'not like''pack_name', [  
  3.         '%sandbox%',  
  4.         'com.famigo%'  
  5.     ]  
  6. ])  
上面程式等同于`pack_name` NOT LIKE '%sandbox%' AND `pack_name` NOT LIKE 'com.famigo%'

LIKE复杂的使用方法请参考IN,都是类似的。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值