旗下导航:搜·么
当前位置:网站首页 > PHP框架 > YII > 正文

yii框架怎样前提查询【yii框架教程】,yii,框架,条件查询

作者:搜搜PHP网发布时间:2019-12-08分类:YII浏览:89


导读:前提查询$customers=Customer::find()->where($cond)->all();$cond就是我们所谓的前提,前提的写法也...

前提查询

$customers = Customer::find()->where($cond)->all();

$cond就是我们所谓的前提,前提的写法也依据查询数据的差别存在差别,那末如何用yii2的体式格局来写查询前提呢?

[[简朴前提]]

// SQL: (type = 1) AND (status = 2).  
$cond = ['type' => 1, 'status' => 2]   
// SQL:(id IN (1, 2, 3)) AND (status = 2)  
$cond = ['id' => [1, 2, 3], 'status' => 2]   
//SQL:status IS NULL  
$cond = ['status' => null]

【and】:将差别的前提组合在一起,用法举例:

//SQL:`id=1 AND id=2`  
$cond = ['and', 'id=1', 'id=2']  
//SQL:`type=1 AND (id=1 OR id=2)`  
$cond = ['and', 'type=1', ['or', 'id=1', 'id=2']]  
//SQL:`type=1 AND (id=1 OR id=2)` //此写法'='能够换成其他操作符,例:in like != >=等  
$cond = [  
    'and',  
    ['=', 'type', 1],  
    [  
        'or',  
        ['=', 'id', '1'],  
        ['=', 'id', '2'],  
    ]  
]

[[or]]:

/SQL:`(type IN (7, 8, 9) OR (id IN (1, 2, 3)))`  
$cond = ['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]

[[not]]:

//SQL:`NOT (attribute IS NULL)`  
$cond = ['not', ['attribute' => null]]

[[between]]: not between 用法雷同

//SQL:`id BETWEEN 1 AND 10`  
$cond = ['between', 'id', 1, 10]

[[in]]: not in 用法相似

//SQL:`id IN (1, 2, 3)`  
$cond = ['in', 'id', [1, 2, 3]] or $cond = ['id'=>[1, 2, 3]]
//IN前提也适用于多字段  
$cond = ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]]  
//也适用于内嵌sql语句  
$cond = ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]

[[like]]:

//SQL:`name LIKE '%tester%'`
$cond = ['like', 'name', 'tester']
//SQL:`name LIKE '%test%' AND name LIKE '%sample%'`
$cond = ['like', 'name', ['test', 'sample']]
//SQL:`name LIKE '%tester'`
$cond = ['like', 'name', '%tester', false]

[[exists]]: not exists用法相似

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

另外,您能够指定恣意运算符以下:

//SQL:`id >= 10`
$cond = ['>=', 'id', 10] 
//SQL:`id != 10`
$cond = ['!=', 'id', 10]

ki4网,有大批免费的Yii入门教程,迎接人人进修!

以上就是yii框架怎样前提查询的细致内容,更多请关注ki4网别的相干文章!

标签:yii框架条件查询