Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Select using Zend Db

I have a query like

select empl.idemp ,(select em.firstnm from tbl_employeemaster em where em.idemp = empl.approvedby) as approvedby from tbl_empleaveapplication empl
join tbl_employeemaster emp on empl.idemp = emp.idemp
join tbl_leavemaster lvm on empl.idleavemaster = lvm.idleavemaster

I need to use zend db to build this type of query

like image 585
Arunvishy Avatar asked Apr 09 '11 11:04

Arunvishy


1 Answers

Here is how you can convert your string query into Zend_Db_Select

class Empleaveapplication extends Zend_Db_Table_Abstract
{
    protected $_name = 'tbl_empleaveapplication';
}

$table = new Empleaveapplication();

// create sub query
$subSql = $table->select()
                ->setIntegrityCheck(false)
                ->from(array('em' => 'tbl_employeemaster'), array('firstnm'))
                ->where('idemp = empl.approvedby', '');

// main query
$sql = $table->select()
             ->setIntegrityCheck(false)
             ->from(array('empl' => 'tbl_empleaveapplication'), array('idemp', 'approvedby' => new Zend_Db_Expr('(' . $subSql . ')')))
             ->joinInner(array('emp' => 'tbl_employeemaster'), 'empl.idemp = emp.idemp', array())
             ->joinInner(array('lvm' => 'tbl_leavemaster'), 'empl.idleavemaster = lvm.idleavemaster', array());
like image 50
satrun77 Avatar answered Sep 23 '22 08:09

satrun77