Yii2如何进行where AND或OR条件分组?

a64a0gku  于 2022-11-09  发布在  其他
关注(0)|答案(8)|浏览(369)

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

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)

谢谢

flvlnr44

flvlnr441#

您可以尝试以下操作:

//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();
x759pob2

x759pob22#

试试这个-

$query = (new \yii\db\Query())
            ->select('*')
            ->from('users u')
            ->where(['and',['u.user_id'=>[1,5,8]],['or','u.status=1','u.verified=1']])
            ->orWhere(['u.social_account'=>1,'u.enable_social'=>1]);
    $command = $query->createCommand();
    print_r ($command->sql);die;

more info

68bkxrlz

68bkxrlz3#

我假设你已经了解了Yii 2.0中的数据库配置,它和Yii 1.0版本中的基本相同。
如果要使用activeQuery,则需要首先定义一个'USERS'类:

<?php
    namespace app\models;
    use yii\db\ActiveRecord;

    class USERS extends ActiveRecord {
        public static function tableName()
        {
            return 'users';
        }
    }
?>

然后,当您使用它时,可以将其编写为:

<?
    $usr_data = USERS::find()->  
            ->where("user_id IN(1,5,8) AND (status = 1 OR verified = 1) OR (social_account = 1 AND enable_social = 1)")
            ->all();    
?>

在我看来,主动查询提供了一种通过子块分离sql的方法。但是当你有这样一个复杂的“AND OR”WHERE条件时,应用它没有任何意义。

kninwzqo

kninwzqo4#

★也可以这样做:

$data = model::find()
->andWhere('plz = :plz',[':plz' => 40])
->andWhere('plz = :plz',[':plz' => 40000])
->all();
7d7tgy0s

7d7tgy0s5#

User::find()
    ->select('u_id , u_unique_id, u_first_name, u_last_name, u_username, u_email, u_image,u_phone') 
    ->andWhere("u_status != 2  AND u_id != 1 and u_role in (6,7)")
    ->andWhere(
        ['or',
            ['like', 'u_first_name', $searchVal],
            ['like', 'u_last_name', $searchVal],
            ['like', 'u_username', $searchVal],
            ['like', 'u_email', $searchVal]

        ]
        )
    ->all();
egmofgnx

egmofgnx6#

使用MongoDB

$query->andWhere(['and',
                ['$eq', 'status', 1],
                ['$in', 'activity', [1, 2]],
            ]);
            $query->orWhere(['$in', 'yourField', $yourArray]);

已测试。与DBMS类似。

w51jfk4q

w51jfk4q7#

首先使用OR条件。例如:

(new \yii\db\Query())
            ->select(['id', 'client', 'ts', 'action'])
            ->from('log_client as log')
            ->orWhere(['action' => 'lock'])
            ->orWhere(['action' => 'rel'])
            ->andWhere(['in', 'client', $IDs])
            ->orderBy(['ts' => SORT_ASC])
            ->all();

就像“...(.....)”

2skhul33

2skhul338#

ActiveQuerywhere()函数也接受字符串作为它的第一个参数,该参数将在WHERE查询条件中使用。因此,最简单的方法是将这些条件放在where()函数中。
假设您使用的是ActiveRecord模型,您可以在模型中执行类似以下的操作。

$this->find()
       ->where("user_id IN(1,5,8) AND (status = 1 OR verified = 1) OR (social_account = 1 AND enable_social = 1)")
       ->all();

当然,您应该使用预准备语句,而不是在查询中硬编码值,但上面的代码只是给予您一个想法。
您可以在文档Here和Here中找到更多详细信息

相关问题