Home Backend Development PHP Tutorial Yii2 add, delete, modify and query query where parameter is introduced in detail

Yii2 add, delete, modify and query query where parameter is introduced in detail

Jan 21, 2017 am 11:17 AM

Overview

Since the official manual has relatively few introductions about where, I would like to organize it myself so that everyone can learn and check back. This article will introduce in detail the usage and examples of and, or, between, in, and like in the where method.

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()方法会将数据以数组的方式显示
Copy after login

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();
Copy after login

between

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

in

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

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();
// 所以,右边也是同样
Copy after login

The above is the relevant knowledge introduced by the editor to you about the detailed introduction of the where parameter of the Yii2 add, delete, modify and query query. I hope it is helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more Yii2 add, delete, modify and query queries where parameters are introduced in detail, please pay attention to the PHP Chinese website for related articles!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles