我正在研究npm install --save body-parser
。我正在使用活动记录来搜索参考号。查询在
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);
条件。原始查询位于
NOT IN
如何将此添加到我的有效记录查询中?
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
将查询更改为:
$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();
OR
$ser = Survey::find()
->where("ref_no like '%$q%' AND ref_no NOT IN (select ref_no from installations where ref_no LIKE '%q%')")
->andWhere(['status'=>1])
->asArray()->all();
答案 1 :(得分:1)
您也可以使用子查询(假设您的安装表与安装模型相关)
$subQuery = Installations::find()->select('ref_no')->where("ref_no like '%$q%'");
$query = Survey::find()->where(['not in', 'ref_no', $subQuery]);
$models = $query->all();