使用Zend_Db Api实现IN子句的位置

时间:2012-06-13 17:38:17

标签: php mysql zend-framework zend-db

我正在尝试使用Zend_Db api编写以下查询:

select * from tableA where id not in (select pid from tableB where x =46 and y='participated');

我编写了以下代码以实现此功能

首先,我从tableB获取数组格式的pid列表:

    $select = $this->select()
    ->from(array('tb' =>'tableB'), array('mylist' => new Zend_Db_Expr('group_concat(tb.pid)' )))
    ->where('x = ?', $xval) //$xval is coming as 46
    ->where('type = ?', 'participated');

    $result = $this->getAdapter()->fetchAll($select);
    error_log("Result of query1 is " . print_r($result, true));

    //Convert to array
    $mylistArr = preg_split('/,/' , $result[0]['mylist'], PREG_SPLIT_NO_EMPTY);
    error_log("value of mylistArr is " . print_r($mylistArr, true));


    //Now get the results from tableA
    $selectta = $this->select()
    ->setIntegrityCheck(false)
    ->from(array('ta' => 'tableA'), array('ta.id', 'ta.first_name', 'ta.last_name'))
    ->where('ta.id not in (?)', $mylistArr[0]);

    $result = $this->fetchAll($selectta);
    error_log("db query result is " . print_r($result, true));

现在,问题是: 正在形成的最终查询是

SELECT `ta`.`id`, `ta`.`first_name`, `ta`.`last_name` FROM `tableA` AS `ta` WHERE (ta.id not in ('197,198,199,200,106,201,202,204,203,205'))

但是,我希望查询看起来如下(也就是说,来自tableB的id列表不应该用引号括起来):

SELECT `ta`.`id`, `ta`.`first_name`, `ta`.`last_name` FROM `tableA` AS `ta` WHERE (ta.id not in (197,198,199,200,106,201,202,204,203,205))

原因是当在IN子句中传递引号时,只有第一个id即197被选中以过滤结果。

非常感谢任何帮助。

由于

3 个答案:

答案 0 :(得分:2)

请查看他们使用ZEND的网站并选择...其中.. IN ...

How to do MySQL IN clauses using Zend DB?

让我们知道它是怎么回事。

答案 1 :(得分:1)

我找到了实现这一目标的一种方法。但不确定这是否是最佳方式: 我修改了以下查询:

   $selectta = $this->select()
    ->setIntegrityCheck(false)
    ->from(array('ta' => 'tableA'), array('ta.id', 'ta.first_name', 'ta.last_name'))
    ->where('ta.id not in (?)', $mylistArr[0]);

要:

  $selectta = $this->select()
    ->setIntegrityCheck(false)
    ->from(array('ta' => 'tableA'), array('ta.id', 'ta.first_name', 'ta.last_name'))
    ->where('not find_in_set(ta.id , ?)', $mylistArr[0]);

它给了我适当的结果。

欢迎任何有关改进此答案的建议。

答案 2 :(得分:1)

我不需要使用preg_split,而是需要使用explode并将逗号分隔的字符串转换为可以传递给where IN子句的id数组。

以下是我做的最终实施:

 $select = $this->select()
    ->from(array('tb' =>'tableB'), array('mylist' => new Zend_Db_Expr('group_concat(tb.pid)' )))
    ->where('x = ?', $xval) //$xval is coming as 46
    ->where('type = ?', 'participated');

    $result = $this->getAdapter()->fetchAll($select);
    error_log("Result of query1 is " . print_r($result, true));

    //Convert to array
    $mylistArr = explode(",", $result[0]['mylist']);
    error_log("value of mylistArr is " . print_r($mylistArr, true));


    //Now get the results from tableA
    $selectta = $this->select()
    ->setIntegrityCheck(false)
    ->from(array('ta' => 'tableA'), array('ta.id', 'ta.first_name', 'ta.last_name'))
    ->where('ta.id not in (?)', $mylistArr);

    $result = $this->fetchAll($selectta);
    error_log("db query result is " . print_r($result, true));