yii2-未处于状态的活动记录

时间:2018-04-13 06:24:41

标签: activerecord yii2 yii2-advanced-app notin

我正在研究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

如何将此添加到我的有效记录查询中?

任何帮助都将受到高度赞赏。

2 个答案:

答案 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();