Yii2。如何建立子表达

时间:2018-11-20 15:54:16

标签: yii2

如何在不使用yii\db\Expression和原始sql的情况下使用子表达式构建查询。例如:

SELECT * FROM user WHERE archived = 3 AND ((group = 2 AND status = 3) OR (group = 3 AND status = 2));

1 个答案:

答案 0 :(得分:1)

您可以使用数组表达式建立这样的条件:

$users = (new Query())
    ->from('user')
    ->where(['archived' => 3])
    ->andWhere([
        'or',
        [
            'group' => 2,
            'status' => 3,
        ],
        [
            'group' => 3,
            'status' => 2,
        ],
    ])
    ->all();