首页 > 语言 > PHP > 正文

Yii2增删改查之查询 where参数详细介绍

2024-05-04 23:49:03
字体:
来源:转载
供稿:网友

概述

由于官方手册关于where的介绍比较少,所以想自己整理一下,以便大家的学习和自己回头查询。本篇文章会详细介绍and、or、between、in、like在where方法中的使用方法和举例。

and

// 我们要查询id大于1并且小于3的数据$userInfo = User::find()->where(['and' , 'id > 1' , 'id < 3'])->all();// 或者用以下方式,更为安全$userInfo = User::find()->where(['and' , ['>' , 'id' , 1] , ['<' , 'id' , 3]])->all();// 往往我们会处理比这更复杂的sql// 假如我们要查询name是王五 并且 id大于1或者id小于3的数据$userInfo = User::find()->where(['and' , ['=' , 'name' , '王五'] , ['or' , ['=' , 'id' , 1] , ['=' , 'id' , 3]]])->asArray()->all();// 注:asArray()方法会将数据以数组的方式显示

or

// 我们要查询id等于1或者id等于3的数据$userInfo = User::find()->where(['or' , 'id = 1' , 'id = 3'])->all();// 我们同样可以使用以下方式$userInfo = User::find()->where(['or' , ['=' , 'id' , 1] , ['=' , 'id' , 3]])->all();// 假如我们要查询id在4,8,9范围内 或者 id在1,2,3范围内呢?$userInfo = User::find()->where(['or' , ['id' => [4,8,9]] , ['id' => [1,2,3]]])->all();

between

// 我们要查询id在1到10的范围之内$userInfo = User::find()->where(['between' , 'id' , 1 , 10])->all();

in

// 我们要查询id在1、2、3的范围内$userInfo = User::find()->where(['in' , 'id' , [1,2,3]])->all();

like

// 我们要查询name中包含“张”这个字符的数据$userInfo = User::find()->where(['like' , 'name' , '张'])->all();// 我们假如要通配name中包含“张”这个字符,而且还得包含“三”这个字符$userInfo = User::find()->where(['like' , 'name' , ['张' , '三']])->all();// 我们假如只需要通配左边即可$userInfo = User::find()->where(['like' , 'name' , '%三' , false])->all();// 所以,右边也是同样

以上所述是小编给大家介绍的Yii2增删改查之查询 where参数详细介绍的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对VeVb武林网网站的支持!


注:相关教程知识阅读请移步到PHP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选