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

前端之家收集整理的这篇文章主要介绍了zend-framework2 – 在Where子句中的Zf2 NOT IN表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图从另一个表中尚未出现的表中获取结果.
为此,我在where子句中使用subQuery和NOT IN表达式.正常的sql工作如下:
  1. SELECT `products`.* FROM `products` WHERE `products`.`pro_id` **NOT IN** (
  2. 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表达式.例如.

  1. //$productIds is an array
  2. //$collectionId is an id
  3.  
  4. $subSelect = $this->getRawsql()->select('product_collection');
  5. $subSelect -> columns(array('pro_id'));
  6. $subSelect -> where(array('coll_id'=>$collectionId));
  7.  
  8. $select = $this->getRawsql()->select('products');
  9. $select -> **where->in**('products.pro_id',$subSelect)
  10. -> where->in('products.pro_id',$productIds);

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

像使用In()一样使用notIn().
见: http://framework.zend.com/apidoc/2.1/classes/Zend.Db.Sql.Predicate.NotIn.html
  1. $subSelect = $this->getRawsql()->select('product_collection');
  2. $subSelect -> columns(array('pro_id'));
  3. $subSelect -> where(array('coll_id'=>$collectionId));
  4.  
  5. $select = $this->getRawsql()->select('products');
  6. $select ->where->notIn('products.pro_id',$subSelect)
  7. ->where->in('products.pro_id',$productIds);

编辑:

或者改用它

  1. $select ->where
  2. ->addPredicate(new Zend\Db\sql\Predicate\Expression('products.pro_id NOT IN (?)',array($subSelect)))

猜你在找的PHP相关文章