[英]How to do a query in CDbCriteria with Not Exists clause?
I need to do the following query in Yii and I like to use a criteria: 我需要在Yii中执行以下查询,并且喜欢使用条件:
select t1.* from table1 t1
where t1.nome like '%somestring%'
and not exists (select 1 from table2 t2
where t2.table1_id = t1.id and t2.some_id in (15,22,1987));
I didn't find a way to add the "not exists" clause in Yii CDbCriteria. 我没有找到在Yii CDbCriteria中添加“不存在”子句的方法。
Where is no standart method for the NOT EXISTS
. 对于NOT EXISTS
来说没有标准的方法。 You should use CDbExpression
class to add condition to criteria: 您应该使用CDbExpression
类为条件添加条件:
$criteria->addCondition(new CDbExpression("not exists (select 1 from table2 t2
where t2.table1_id = t1.id and t2.some_id in (15,22,1987))"));
I solve the question the following way. 我通过以下方式解决问题。 It's not very beautiful, but it works. 它不是很漂亮,但是可以用。 :) :)
$criteria = new CDbCriteria();
$criteria->addInCondition('t2.some_id', $array_values);
$sql = " select t1.* from table1 t1";
$sql .= " where t1.name like \"%".$string."%\" ";
$sql .= " and not exists (select 1 from table2 t2";
$sql .= " where t2.table1_id = t1.id and " . $criteria->condition . ")";
$connection = Yii::app()->db;
$command = $connection->createCommand($sql);
$result = $command->queryAll(true, $criteria->params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.