将SQL查询转换为Zend DB Select

时间:2015-03-10 11:19:41

标签: php mysql zend-framework

如何将此查询转换为Zend数据库查询

SELECT t1.date, t1.station, t1.ftype, t1.price, t1.currency 
FROM fueling as t1  
INNER JOIN 
(SELECT MAX(t2.date) as maxdate, t2.station, t2.ftype 
FROM fueling as t2   
GROUP BY t2.station, t2.ftype) as t3
ON t1.date=t3.maxdate AND t1.station=t3.station AND t1.ftype=t3.ftype
where t1.station in ('GF112','GF11')
ORDER by t1.station,t1.ftype;

1 个答案:

答案 0 :(得分:0)

尝试以下

$subselect = $this->db->select()
        ->from(array('t2'       => 'fueling'),
               array(
                    'maxdate' => 'MAX(t2.date)',
                    'station' => 't2.station',
                    'ftype' => 't2.ftype',
            ));

$select = $this->db->select()
            ->from(array('t1'       => 'fueling'),
                   array(
                        'date' => 't1.date',
                        'station' => 't1.station',
                        'ftype' => 't1.ftype',
                        'price'=>'t1.price',
                        'currency'=>'t1.currency'
                ))
            ->JOIN(array('t3'=>$subselect),'t1.date=t3.maxdate and t1.station=t3.station and t1.ftype=t3.ftype')
            ->Where('t1.station IN (?)', array('GF112','GF11')');

$results = $this->db->query($select)->fetchAll();