Yii的Where条件

#标准写法

$stat_book_list = BookSaleChangeLog::find()
->select( [ 'book_id','SUM(quantity) AS total_count','SUM(price) AS total_pay_money' ] )
->andWhere([  'between','created_time',$time_start,$time_end ])
->groupBy('book_id')->asArray()->all();


$query = Project::find()
            ->where(['dept_id'=>$dept_id])
            ->all();

#写完之后一定要加上one()或者是all()


#对比
['>', 'id', 1]                                        // 生成:id > 1
['<', 'id', 100]                                      // 生成:id < 100
['=', 'id', 10]                                       // 生成:id = 10
['>=', 'id', 1]                                       // 生成:id >= 1
['<=', 'id', 100]                                     // 生成:id <= 100
['!=', 'id', 10]                                      // 生成:id != 10
 
#并联
['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)
 
#OR
['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]]   
// 生成:(type IN (7, 8, 9) OR (id IN (1, 2, 3)))

#NOT
['not', ['attribute' => null]]                       // 生成:NOT (attribute IS NULL)
 
#between
['between', 'id', 1, 10]                             // 生成:id BETWEEN 1 AND 10
['not between', 'id', 1, 10]                         // 生成:id NOT BETWEEN 1 AND 10
 
#IN
['in', 'id', [1, 2, 3]]                               // 生成:id IN (1, 2, 3)
['id' => [4, 8, 15]]								  // 生成:id IN (4, 8, 15)
['not in', 'id', [1, 2, 3]]                           // 生成:id NOT IN (1, 2, 3)
['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])]
 
#模糊查询
['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,并且自行添加%

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值