Activerecord yii2活动记录不在状态
我正在做yii2。我正在使用活动记录搜索参考号。查询如下Activerecord yii2活动记录不在状态,activerecord,yii2,yii2-advanced-app,notin,Activerecord,Yii2,Yii2 Advanced App,Notin,我正在做yii2。我正在使用活动记录搜索参考号。查询如下 $q = isset($_GET['q']) ? $_GET['q'] : ''; if(empty($q)) exit; $ser = Survey::find()->where("ref_no like '%$q%'")->andWhere(['status'=>1])->asArray()->all(); return json_encode($ser); 上述查询将获得调查
$q = isset($_GET['q']) ? $_GET['q'] : '';
if(empty($q)) exit;
$ser = Survey::find()->where("ref_no like '%$q%'")->andWhere(['status'=>1])->asArray()->all();
return json_encode($ser);
上述查询将获得调查表中的所有参考号。现在,我想添加一个不在状态。下面是原始查询
。。。。。。其中ref_no LIKE“%$q%”不在从ref_no LIKE“%q%”的安装中选择ref_no
如何将其添加到活动记录查询中
非常感谢您的帮助。请按以下方式更改您的查询:
$ser = Survey::find()->where("ref_no like '%$q%'")
->andWhere(['status'=>1])
->andWhere("ref_no NOT IN (select ref_no from installations where ref_no LIKE '%q%')")
->asArray()->all();
或
更改您的查询,如下所示:
$ser = Survey::find()->where("ref_no like '%$q%'")
->andWhere(['status'=>1])
->andWhere("ref_no NOT IN (select ref_no from installations where ref_no LIKE '%q%')")
->asArray()->all();
或
假设您的安装表i与安装模型相关,您也可以为此使用子查询
$subQuery = Installations::find()->select('ref_no')->where("ref_no like '%$q%'");
$query = Survey::find()->where(['not in', 'ref_no', $subQuery]);
$models = $query->all();
假设您的安装表i与安装模型相关,您也可以为此使用子查询
$subQuery = Installations::find()->select('ref_no')->where("ref_no like '%$q%'");
$query = Survey::find()->where(['not in', 'ref_no', $subQuery]);
$models = $query->all();
可能重复的可能重复这是一个更整洁的方法+1,因为我也使用相同的方法⌐■_■@穆罕默德·默拉斯拉姆。。。谢谢该方法是否基于yii2指南这是一种更整洁的方法+1,因为我也使用相同的方法⌐■_■@穆罕默德·默拉斯拉姆。。。谢谢该方法是否基于yii2指南