Yii2.0中文开发向导Where条件查询全解析,
Yii2.0中文开发向导——Where条件查询全解析,
在Yii的Model里进行查询的时候 where是必不可少的。
Where方法声明为
其中参数 $condition类型为字符串或者数组
1、字符串
字符串是最简单的,直接按sql中的where条件写就可以,如
2、数组
如果是数组的情况下,有两种格式的写法。
- name-value格式的字典数组:['column1' => value1, 'column2' => value2, ...]
- 逻辑操作符格式:[operator, operand1, operand2, ...]
第一种写法:
如果value值是字符串或者数字等,那么生成的条件语句格式为column1=value1 AND column2=value2 AND ....
['type' => 1, 'status' => 2]
//生成
(type = 1) AND (status = 2)
如果value值是数组,那么会生成sql 中的IN语句;
['id' => [1, 2, 3], 'status' => 2]
//生成
(id IN (1, 2, 3)) AND (status = 2)
如果value值为Null,那么会生成 Is Null语句。
['status' => null]
//生成
status IS NULL
第二种写法会根据不同的操作符生成不同的sql条件。
-
and: 会使用 AND把所有的操作数连接起来。如
['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: 和 and 类似,只不过是用 OR来连接操作数。
-
between: 第一个操作数是列的名称,第二个和第三个操作数为范围的最小值和最大值。如
['between', 'id', 1, 10]
//生成
id BETWEEN 1 AND 10
- not between: 和between 相似。
-
in: 第一个操作数为列或者DB表达式,第二个操作数为数组, 如
['in', 'id', [1, 2, 3]]
//生成
id IN (1, 2, 3)
注意:这个方法会对列进行引用,对数组中的值也会编码。 - not in: 和上面的in 相似。
-
like: 第一个操作数为列或者DB表达式,第二个操作数为字符串或者数组如
['like', 'name', 'tester']
//生成
name LIKE '%tester%'
如果值是数组的话,会生成多个like语句,并用 AND来连接。如['like', 'name', ['test', 'sample']]
//生成
name LIKE '%test%' AND name LIKE '%sample%'
注意:这个方法会对列进行引用,对数组中的值也会编码。
有时候你可能需要自己来处理%,那么可以用第三个参数:['like', 'name', '%tester', false]
//生成
name LIKE '%tester'
- or like: 和like相似,只是在第二个参数为数组的情况下用or来连接多个like 语句。
- not like: 和like 相似。
- or not like: 和or like 相似。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head
