php - 将 SQL 查询转换为 Zend DB Select

标签 php mysql zend-framework

如何将此查询转换为 Zend DB 查询

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;

最佳答案

尝试下面的一个

$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();

关于php - 将 SQL 查询转换为 Zend DB Select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28962345/

相关文章:

php - laravel 4.2 对复杂查询进行分页

php - 实现 Zend Framework 2 自动加载器

php - 从 mysql 检索一条记录并将其存储在 PHP 的数组中

php - Mysql 和 PHP - 从不同的服务器更新数据库

php - 使用 php 运行 MYSQL SELECT 查询

javascript - 我们可以从 html 文本框中调用 java 函数吗?

zend-framework - Zend_Auth 的多个实例

php - json多维数组和jquery?

php - 检查多维数组的每个子数组是否包含元素

php - 从 php 调用嵌套存储过程