Ver Mensaje Individual
  #2 (permalink)  
Antiguo 06/12/2011, 07:44
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: hacer una subcosulta con Zend_Db_Expr

Podría ser algo así
Código PHP:
Ver original
  1. $max = $this->getAdapter()
  2.     ->select()
  3.     ->from(array('msj' => 'mensaje'), new Zend_Db_Expr('MAX(`msj`.`fecha`) as max_time'))
  4.     ->group('msj.de');
  5. echo $this->getAdapter()
  6.     ->select()
  7.     ->from(array('m' => 'mensaje'))                    
  8.     ->join(array('u' => 'usuario'), 'm.de = u.id', array('nombre'=>'nombre', 'apellido'=>'apellido'))
  9.     ->join(array('tmp' => new Zend_Db_Expr("($max)")), 'm.fecha = tmp.max_time')
  10.     ->where('u.rol = ?', 0)                    
  11.     ->where('m.para = ?', (int)$id)                    
  12.     ->group('m.de')
  13.     ->order('m.fecha DESC')
  14.     ->__toString();
Como también puedes indicar directamente la consulta en Zend_Db_Expr.
Código PHP:
Ver original
  1. echo $this->getAdapter()
  2.     ->select()
  3.     ->from(array('m' => 'mensaje'))                    
  4.     ->join(array('u' => 'usuario'), 'm.de = u.id', array('nombre'=>'nombre', 'apellido'=>'apellido'))
  5.     ->join(array('tmp' => new Zend_Db_Expr("(SELECT MAX(`msj`.`fecha`) as max_time FROM `mensaje` AS `msj` GROUP BY `msj`.`de`)")), 'm.fecha = tmp.max_time')
  6.     ->where('u.rol = ?', 0)                    
  7.     ->where('m.para = ?', (int)$id)                    
  8.     ->group('m.de')
  9.     ->order('m.fecha DESC')
  10.     ->__toString();
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos

Última edición por abimaelrc; 06/12/2011 a las 07:52