Yii2查詢之where條件拼裝


目錄

    1 語法
    2 哈希格式
    3 運算符格式
        3.1 對比
        3.2 and
        3.3 or
        3.4 not
        3.5 between和not between
        3.6 in和not in
        3.7 like
        3.8 exists

熟悉Yii2的查詢條件后,用Active Record查詢數據非常方便。

以下我們介紹where()方法當中,條件的拼裝方式。
1 語法

Yii2用where()方法(當然還有其他方法)來實現條件篩選,語法:

public $this where ( $condition, $params = [] )

$params為可選參數,指定要綁定查詢的值。

$condition為必選參數$condition可以是字符串(如'id=1')或者數組。

$condition為數組時,有兩種格式:

    哈希格式:['column1' => value1, 'column2' => value2, ...]
    運算符格式:[operator, operand1, operand2, ...]

2 哈希格式

通常,哈希格式的查詢條件生成這樣的SQL語句:

column1=value1 AND column2=value2 AND ...

如果某個值是數組,就會生成IN語句。

如果某個值為null,會用IS NULL來生成語句。

例子:

['type' => 1, 'status' => 2]            // 生成:(type = 1) AND (status = 2)
['id' => [1, 2, 3], 'status' => 2]      // 生成:(id IN (1, 2, 3)) AND (status = 2)
['status' => null]                      // 生成:status IS NULL

3 運算符格式

在運算符格式,Yii會根據指定的運算符生成SQL語句。

運算符有:and、or、not、between、not between、in、not in、like、or like、not like、or not like、exists、not exists、>、<、=、>=、<=、!=等。
3.1 對比

['>', 'id', 1]                                        // 生成:id > 1
['<', 'id', 100]                                      // 生成:id < 100
['=', 'id', 10]                                       // 生成:id = 10
['>=', 'id', 1]                                       // 生成:id >= 1
['<=', 'id', 100]                                     // 生成:id <= 100
['!=', 'id', 10]                                      // 生成:id != 10

具體生成的SQL語句,運算符id會自動加上反斜杠引號`,運算數會自動轉義。
3.2 and

['and', 'id' => 1, 'id' => 2]                        // 生成:id=1 AND id=2
['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)

在第2條和第3條語句中,列名稱和搜索值未用鍵值關系指定,所以生成的SQL不會添加引號,也不會轉義。
3.3 or

['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]]   // 生成:(type IN (7, 8, 9) OR (id IN (1, 2, 3)))

3.4 not

['not', ['attribute' => null]]                       // 生成:NOT (attribute IS NULL)

3.5 between和not between

['between', 'id', 1, 10]                             // 生成:id BETWEEN 1 AND 10
['not between', 'id', 1, 10]                         // 生成:id NOT BETWEEN 1 AND 10

運算符后面的運算數1為數據表列名稱,運算數2和運算數3分別為列值范圍的最小值和最大值。
3.6 in和not in

['in', 'id', [1, 2, 3]]                               // 生成:id IN (1, 2, 3)
['not in', 'id', [1, 2, 3]]                           // 生成:id NOT IN (1, 2, 3)

運算符后面的運算數1為列名稱或DB表達式,運算數2為數組,指定列值所在的范圍。

這個方法會為值添加引號,並正確轉義。

要生成混合IN條件,列名和列值都設置為數組,並且用列名為列值指定下標:

['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]]  // 生成:(`id`, `name`) IN ((1, 'foo'), (2, 'bar'))

另外,還可以用子查詢作為IN條件的值,如下:

['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]

3.7 like

['like', 'name', 'tester']                             // 生成:name LIKE '%tester%'
['like', 'name', ['test', 'sample']]                   // 生成:name LIKE '%test%' AND name LIKE '%sample%'
['like', 'name', '%tester', false]                     // 生成:name LIKE '%tester'
                                                       // 這是自定義查詢方式,要傳入值為false的運算數3,並且自行添加%

運算數后面的運算數1為列名稱或DB表達式,運算數2為字符串或數組,指定列值查詢條件。

這個方法會為值添加引號,並正確轉義。

or like、not like、or not like用法和like一樣。

['or like', 'name', ['test', 'sample']]                 // 生成:name LIKE '%test%' OR name LIKE '%sample%'
['not like', 'name', 'tester']                          // 生成:name NOT LIKE '%tester%'
['or not like', 'name', ['test', 'sample']]             // 生成:name NOT LIKE '%test%' OR name NOT LIKE '%sample%'

3.8 exists

['exists', (new Query())->select('id')->from('users')->where(['active' => 1])] // 生成:EXISTS (SELECT "id" FROM "users" WHERE "active"=1)

not exists用法和exists一樣。

 

參考地址:

    http://www.yiiframework.com/doc-2.0/yii-db-query.html#where()-detail

來源:https://www.awaimai.com/1897.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM