Search code examples
sqlzend-frameworkzend-db

How to make this query in Zend DB?


I'm trying to have Zend DB generate the following query:

SELECT DISTINCT
    US.nombre AS Cliente,
    VE.id_venta,
    VE.fecha,
    VE.total,
    ve.observacion
FROM
    usuarios AS US,
    ventas AS VE

I have tried the code below, but it does not work:

$select = $this->select();
$select->from(array('VE' => 'ventas'), array('id_venta', 'fecha', 'total', 'observacion'))
       ->from(array('US' => 'usuarios'),'ve.id_usuario');

Solution

  • I think this is what you're looking for:

    $select = $this->select();
    $select->from(array('US' => 'usuarios'), array('US.nombre as Cliente'))
           ->from(array('VE' => 'ventas'), array('VE.id_venta', 'VE.fecha', 'VE.total', 'VE.observacion'));
    

    This gives me the following output:

    SELECT `US`.`nombre` AS `Cliente`, `VE`.`id_venta`, `VE`.`fecha`, `VE`.`total`, `VE`.`observacion` FROM `usuarios` AS `US` INNER JOIN `ventas` AS `VE`