一尘不染

Yii2如何执行“与”或“或”条件分组?

mysql

我是Yii-2框架的新手。我如何使用activeQuery和模型在Yii-2框架中实现以下查询。

SELECT * FROM users AS u WHERE u.user_id IN(1,5,8) AND (u.status = 1 OR u.verified = 1) OR (u.social_account = 1 AND u.enable_social = 1)

谢谢


阅读 226

收藏
2020-05-17

共1个答案

一尘不染

您可以尝试以下方法:

//SELECT * FROM users AS u WHERE u.user_id IN(1,5,8) AND (u.status = 1 OR u.verified = 1) OR (u.social_account = 1 AND u.enable_social = 1)
$model = arname()->find()
       ->andWhere(['user_id'=>[1,5,8]])
       ->andWhere(['or',
           ['status'=>1],
           ['verified'=>1]
       ])
       ->orWhere(['and',
           ['social_account'=>1],
           ['enable_social'=>1]
       ])
       ->all();
2020-05-17