加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

zend-framework2 – 在Where子句中的Zf2 NOT IN表达式

发布时间:2020-12-13 18:11:33 所属栏目:PHP教程 来源:网络整理
导读:我试图从另一个表中尚未出现的表中获取结果. 为此,我在where子句中使用subQuery和NOT IN表达式.正常的sql工作如下: SELECT `products`.* FROM `products` WHERE `products`.`pro_id` **NOT IN** (SELECT `product_collection`.`pro_id` AS `pro_id` FROM `p
我试图从另一个表中尚未出现的表中获取结果.
为此,我在where子句中使用subQuery和NOT IN表达式.正常的sql工作如下:
SELECT `products`.* FROM `products` WHERE `products`.`pro_id` **NOT IN** (
SELECT `product_collection`.`pro_id` AS `pro_id` FROM `product_collection` WHERE `coll_id` = '6' ) AND products.pro_id IN (55,56,57,62)

我在项目中使用zf2.问题是我不知道如何在where子句中使用NOT IN表达式.我们可以在Where子句中使用where-> in()作为IN表达式.例如.

//$productIds is an array
//$collectionId is an id

$subSelect = $this->getRawSql()->select('product_collection');
$subSelect -> columns(array('pro_id'));
$subSelect -> where(array('coll_id'=>$collectionId));

$select = $this->getRawSql()->select('products');
$select -> **where->in**('products.pro_id',$subSelect)
        -> where->in('products.pro_id',$productIds);

但我不知道如何使用NOT IN表达式.

像使用In()一样使用notIn().
见: http://framework.zend.com/apidoc/2.1/classes/Zend.Db.Sql.Predicate.NotIn.html
$subSelect = $this->getRawSql()->select('product_collection');
$subSelect -> columns(array('pro_id'));
$subSelect -> where(array('coll_id'=>$collectionId));

$select = $this->getRawSql()->select('products');
$select ->where->notIn('products.pro_id',$subSelect)    
        ->where->in('products.pro_id',$productIds);

编辑:

或者改用它

$select ->where
        ->addPredicate(new ZendDbSqlPredicateExpression('products.pro_id NOT IN (?)',array($subSelect)))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读