Abhishek Patidar
Abhishek Patidar

Reputation: 1577

Apply AND condition at join in zend framework

Hello I have query in which I have to apply and condition in join place, But I don't know how to do this in zend. Please help.

$dbA = $this->dbAdapter;
        $sql = new Sql($dbA);
        $select = $sql->select()->from(array('answer' => 'tblanswer'));
        $select->columns($fetch);
        $select->join(array('fixed' => 'tblfixanswer'), 'fixed.aid = answer.aid', array(), 'left');
        $selectString = $sql->getSqlStringForSqlObject($select);
        $results = $dbA->query($selectString, $dbA::QUERY_MODE_EXECUTE);

The desired out needed to be:

SELECT *
FROM `tblanswer` AS `answer`
LEFT JOIN `tblfixanswer` AS `fixed` ON `fixed`.`aid` = `answer`.`aid` and fixed.baid=27

The value 27 is coming from a variable.

Upvotes: 1

Views: 1087

Answers (1)

Ronak K
Ronak K

Reputation: 1567

You can easily do that as,

dbA = $this->dbAdapter;
    $sql = new Sql($dbA);
    $select = $sql->select()->from(array('answer' => 'tblanswer'));
    $select->columns($fetch);
    $select->join(array('fixed' => 'tblfixanswer'), 'fixed.aid = answer.aid AND fixed.baid=27', array(), 'left');
    $selectString = $sql->getSqlStringForSqlObject($select);
    $results = $dbA->query($selectString, $dbA::QUERY_MODE_EXECUTE);

Hope that helps..

Upvotes: 2

Related Questions