1,多条件查询
//方法1
->where('is_delete = :c1 AND gg.id IN :c2',[':c1'=>0,':c2'=>$goods_list_id]);
//方法2
->where(['is_delete'=>0,'gg.id'=>$goods_list_id]); //in的话可以这样
//方法3
->where(
[
'and',
['is_delete'=>0],
['in','gg.id'=>$goods_list_id]
]
);
2,直接sql语句执行
$sql = "UPDATE tb_table SET not_num = should_num - already_num WHERE id = {$id}";
Yii::$app->db->createCommand($sql)->execute();
3,结果集+join+union组合
# 结果集1
$query1 = new Query();
$query1 -> select(['id AS train_id','train_theme AS train_name','start_date','user_id','admin_time AS audit_time','IFNULL(flag,1) AS flag']);
$query1 -> from(OutTrain::tableName());
$query1 -> where(['user_id'=>$userId,'audit_status'=>1]);
# 结果集2
$query2 = new Query();
$query2 -> select(['ut.train_id','t.train_name','t.start_date','ut.user_id','ut.created_at AS audit_time','IFNULL(flag,0) AS flag']);
$query2 -> from(['ut'=>UsersTrain::tableName()]);
$query2 -> leftJoin(['t'=>Train::tableName()],'ut.train_id=t.train_id');
$query2 -> where(['ut.user_id'=>$userId]);
# 合并查询
$query3 = new Query();
$query3 -> from(['s'=>$query1->union($query2,'all')]); //union all
$query3 -> where("s.start_date-".time().">86400");//超过1天的显示
$query3 -> orderBy(['s.start_date'=>SORT_ASC]);
$count = $query3 -> count();
$res = $query3 -> limit($limit) -> offset($offset) -> all();