【问题标题】:How to add IN statement with JOIN ON clause in tablegateway ZF2如何在 tablegateway ZF2 中添加带有 JOIN ON 子句的 IN 语句
【发布时间】:2013-03-02 08:38:01
【问题描述】:

在我添加带有 join ON 子句的“IN”语句之前获取所有结果

我的代码是这样的:

// $this->table = 'category';
$sql = $this->getSql();
$select = $sql->select();
$select->join('user_category_subscriptions', 'user_category_subscriptions.category_id = category.id AND user_category_subscriptions.status IN (1,2,3,4)', array(), 'left');
$select->where(array('category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))'));

错误

ZEND\DB\ADAPTER\EXCEPTION\INVALIDQUERYEXCEPTION
File:E:\htdocs\myproj\vendor\zendframework\zendframework\library\Zend\Db\Adapter\Driver\Pdo\Statement.php:245
Message:Statement could not be executed

使用'echo $select->getSqlString();'打印查询:

SELECT "categories".* FROM "categories" 
LEFT JOIN "user_category_subscriptions" 
ON "user_category_subscriptions"."category_id" = "categories"."category_id" AND "user_category_subscriptions"."status" IN ("1""," "2""," "3""," "4")
WHERE category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))

所以问题是 zend 自动将 (1,2,3,4) 转换成 ("1""," "2""," "3""," " 4")

有解决这个问题的办法吗?谢谢

【问题讨论】:

    标签: database join zend-framework2 in-clause


    【解决方案1】:

    我认为你可以传递一个表达式而不是连接字符串:

    // $this->table = 'category';
    $sql = $this->getSql();
    $select = $sql->select();
    
    $join = new Expression('user_category_subscriptions.category_id = category.id AND user_category_subscriptions.status IN (1,2,3,4)');
    
    $select->join('user_category_subscriptions', $join, array(), 'left');
    $select->where(array('category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))'));
    

    【讨论】:

      【解决方案2】:

      试试这个

      $select->where
             ->AND->NEST->equalTo('category.user_id', 2)
             ->addPredicate(new Sql\Predicate\In('user_category_subscriptions.status', array(2,4)));
      

      【讨论】:

      • 感谢您的回复!请参阅带有 JOIN ON 子句的 'IN' 语句(即喜欢... ON foo.id=bar.fooid AND foo.barid IN (1,2,3))。只有带有 ON 的“IN”语句会造成所有麻烦,而不是带有 WHERE 的 IN 语句。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多