php Yii2 如何执行 where AND 或 OR 条件分组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31557302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii2 How to perform where AND or OR condition grouping?
提问by Vikas Chaudhary
I am new to Yii-2 framework. How can i achieve following query in Yii-2 framework using activeQuery and models.
我是 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)
Thanks
谢谢
回答by gouki
You can try this:
你可以试试这个:
//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();
回答by mohit
回答by fsn
U can also do it this way:
你也可以这样做:
$data = model::find()
->andWhere('plz = :plz',[':plz' => 40])
->andWhere('plz = :plz',[':plz' => 40000])
->all();
回答by cedricliang
I assume that you have already knew about database configuration in Yii 2.0, which is basically the same as in Yii 1.0 version.
假设你已经知道 Yii 2.0 中的数据库配置,它与 Yii 1.0 版本中的基本相同。
If you want to use activeQuery, you need to define a ‘USERS' class first:
如果你想使用activeQuery,你需要先定义一个'USERS'类:
<?php
namespace app\models;
use yii\db\ActiveRecord;
class USERS extends ActiveRecord {
public static function tableName()
{
return 'users';
}
}
?>
Then when you use it,you can write it as following:
那么在使用的时候,可以这样写:
<?
$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();
?>
In my opinion, active query provides you a way to separate sql by sub-blocks. But it does not make any sense to apply it when you have such a complicated 'AND OR' WHERE condition..
在我看来,主动查询为您提供了一种按子块分隔 sql 的方法。但是,当您有如此复杂的“AND OR”WHERE 条件时,应用它没有任何意义。
回答by ThangTD
With MongoDB
:
与MongoDB
:
$query->andWhere(['and',
['$eq', 'status', 1],
['$in', 'activity', [1, 2]],
]);
$query->orWhere(['$in', 'yourField', $yourArray]);
Tested. Similar with DBMS.
测试。与 DBMS 类似。
回答by Tahir
where()
function of ActiveQuery
also accepts string as its first parameter that will be used in WHERE
condition of the query. So easiest way would be to put those conditions in where()
function.
where()
函数ActiveQuery
也接受字符串作为它的第一个参数,该参数将WHERE
在查询条件下使用。所以最简单的方法是将这些条件放在where()
函数中。
Assuming you are using ActiveRecord
model, you can do something like following in your model.
假设您正在使用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();
Of course you should use prepared statements instead of hardcoding values inside query, but above code is just to give you an idea.
当然你应该在查询中使用准备好的语句而不是硬编码值,但上面的代码只是给你一个想法。
回答by Andreykin Sergey
Use ORcondition at first. For example:
首先使用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();
It'll be like a "AND...(..OR..)"
它会像一个“ AND...(.. OR..)”
回答by Prahlad
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();