Mysql
 sql >> Base de données >  >> RDS >> Mysql

comment construire une sélection imbriquée sur zend db

Vous devez utiliser Zend_Db_Expr objets dans votre requête et structures de tableaux pour sélectionner AS .

ci-dessous est la solution que vous recherchez :

<?php

$db = Zend_Db_Table::getDefaultAdapter();

//  inner query
$sqlSalesRepTotal = $db->select()
        ->from(array('ps' => 'profile'))
        ->joinLeft(array('xbp' => 'xref_store_profile_brand'), 'xbp.profile_id = ps.profile_id')
        ->where('xbp.brand_id = b.brand_id')
        ->where('ps.role = ?', 'salesrep')
        ->where('xbp.store_id IS NULL');

//  main query
$sql = $db->select()
        ->from(array('b' => 'brand'), array(
            //  NOTE: have to add parentesis around the expression
            'salesrepTotal' => new Zend_Db_Expr("($sqlSalesRepTotal)")
        ))
        ->where('....')
        ->group('brand_id');


//  debug
var_dump($db->fetchAll($sql));