search
Homephp教程php手册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 相似。
Statement
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.