Search code examples
phpmysqlzend-frameworkzend-db

Implementing where IN clause using Zend_Db Api's


I am trying to code the following query using Zend_Db api's:

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

I wrote the following code to get this to work:

First I am getting the list of pid's in array format from tableB:

    $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));

Now, the problem is this: The final query being formed is

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'))

However, I want the query to look as follows (that is, the list of id's from tableB should not be enclosed in inverted commas):

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))

The reason is that when inverted commas are being passed in the IN clause, only the first id ie 197 is being picked up to filter the results.

Any help is really appreciated.

Thanks


Solution

  • Instead of using preg_split, I needed to use explode and convert the comma delimited string to an array of id's that could be passed to the where IN clause.

    The following is the final implementation I did:

     $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));