Yii2 where使用

大飛_dafei發表於2018-10-09

 

Yii2 查詢運算子 

        // 運算子有
        // and、or、not、
        // between、not between、
        // in、not in、
        // like、or like、not like、or not like、
        // exists、not exists、
        // >、<、=、>=、<=、!=

 

1) and 查詢

        //  and 查詢
        //  SELECT * FROM `user` WHERE id=1 and status=10
        $model = User::find()->where('id=:id and status=:status', [
            ':id' => 1, ':status' => 10
        ])->all();

2) or 查詢

        //  or 查詢
        //  SELECT * FROM `user` WHERE id=1 or status=9
        $model = User::find()->where('id=:id or status=:status', [
            ':id' => 1, ':status' => 9
        ])->all();

3) and or 查詢

        // and or 查詢
        // SELECT * FROM `user` WHERE status=10 and (id=1 or id=2)
        $model = User::find()->where('status=:status and (id=:id or id=:sid)', [
            ':status' => 10,
            ':id' => 1,
            ':sid' => 2
        ])->all();


        // and or 查詢
        // SELECT * FROM `user` WHERE (status=10) AND ((id=1) OR (id=2))
        $model = User::find()->where(['and','status=10',['or','id=1','id=2']])->all();

4) like 查詢

        //  like 查詢
        //  SELECT * FROM `user` WHERE `username` LIKE '%張%'
        $model = User::find()->where(['like', 'username', "張"])->all();

5) or like 查詢

        // or like 查詢
        // SELECT * FROM `user` WHERE `ssusername` LIKE '%張%' OR `username` LIKE '%李%'
        $model = User::find()->where(['or like', 'username', ['張','李']])->all();

 官網地址

相關文章