1,多条件查询

    1. //方法1
    2. ->where('is_delete = :c1 AND gg.id IN :c2',[':c1'=>0,':c2'=>$goods_list_id]);
    3. //方法2
    4. ->where(['is_delete'=>0,'gg.id'=>$goods_list_id]); //in的话可以这样
    5. //方法3
    6. ->where(
    7. [
    8. 'and',
    9. ['is_delete'=>0],
    10. ['in','gg.id'=>$goods_list_id]
    11. ]
    12. );

    2,直接sql语句执行

    1. $sql = "UPDATE tb_table SET not_num = should_num - already_num WHERE id = {$id}";
    2. Yii::$app->db->createCommand($sql)->execute();

    3,结果集+join+union组合

    1. # 结果集1
    2. $query1 = new Query();
    3. $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']);
    4. $query1 -> from(OutTrain::tableName());
    5. $query1 -> where(['user_id'=>$userId,'audit_status'=>1]);
    6. # 结果集2
    7. $query2 = new Query();
    8. $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']);
    9. $query2 -> from(['ut'=>UsersTrain::tableName()]);
    10. $query2 -> leftJoin(['t'=>Train::tableName()],'ut.train_id=t.train_id');
    11. $query2 -> where(['ut.user_id'=>$userId]);
    12. # 合并查询
    13. $query3 = new Query();
    14. $query3 -> from(['s'=>$query1->union($query2,'all')]); //union all
    15. $query3 -> where("s.start_date-".time().">86400");//超过1天的显示
    16. $query3 -> orderBy(['s.start_date'=>SORT_ASC]);
    17. $count = $query3 -> count();
    18. $res = $query3 -> limit($limit) -> offset($offset) -> all();