我需要在Yii中执行以下查询,我喜欢使用标准:
156f9e6~1
我没有找到一种方法来添加"不存在" Yii CDbCriteria中的条款。
答案 0 :(得分:2)
NOT EXISTS
没有标准方法。您应该使用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))"));
答案 1 :(得分:0)
我用以下方式解决问题。它不是很漂亮,但它有效。 :)
$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);