Yii2 where 條件 整理

2021-09-05 12:27:27 字數 1248 閱讀 9482

yii2 where 涉及的幾種形式

where、 addparams 、filterwhere 、andwhere、orwhere、 andfilterwhere()、 orfilterwhere()、andfiltercompare()

....

字串格式,例如:『status=1』

雜湊格式,例如: [『status』 => 1, 『type』 => 2]

操作符格式,例如:[『like』, 『name』, 『test』]

第一種最簡單的就是上面提到的例子

andwhere([

'like'

,'title'

,'yii'])

;...

where

(`title` like

'%yii%'

)第二種

addwhere([

'and'

,'id=1'

,'parent_id=1'])

;...

where id=

1and parent_id=

1第三種

addwhere([

'and'

,'type=1',[

'or'

,'id=1'

,'parent_id=1']]

);..

.where type=

1and

(id=

1or parent_id=1)

;第四種

->

andwhere([

'or like'

,'name',[

'辣醬'

,'spicy']]

);where `name` like

'%辣醬%'

or `name` like

'%spicy%';

第五種addwhere([

'or',[

'like'

,'name'

,'辣醬'],

['like'

,'title'

,'spicy']]

);//操作符格式的巢狀..

.where

(`status`=1)

and(

(`name` like

'%辣醬%')or

(`title` like

'%spicy%'

))

官方文件:

Yii2查詢之where條件拼裝

2017 01 22 4282瀏覽記錄 目錄 熟悉yii2的查詢條件後,用active record查詢資料非常方便。以下我們介紹where 方法當中,條件的拼裝方式。yii2用where 方法 當然還有其他方法 來實現條件篩選,語法 public this where condition,para...

Yii2查詢之where條件拼裝

目錄 1語法2雜湊格式 3運算子格式 3.1對比 3.2and 3.3or 3.4not 3.5between和not between 3.6in和not in 3.7like 3.8exists 熟悉yii2的查詢條件後,用active record查詢資料非常方便。以下我們介紹where 方法當...

yii2 0框架where條件的使用

在yii框架中,where條件的使用多種多樣,下面就和大家介紹幾種常用有效的使用方法 1 type 1,status 2 等於 type 1 and status 2 2 id 1,2,3 status 2 等於 id in 1,2,3 and status 2 3 status null 等於 s...