0

我有这个我试图通过 Zend_DB 执行的原始 sql 语句。

$sql = 'SELECT relocationaction.id, relocationaction.vehicle, relocationaction.start,         relocationaction.end, relocationaction.return ' .
            'FROM relocationaction,
              (SELECT vehicle, MAX(end) AS maxend
              FROM relocationaction
              GROUP BY vehicle) AS co2
            WHERE co2.vehicle = relocationaction.vehicle
            AND(relocationaction.monitor = 1)
            AND (relocationaction.return IS NULL)
            AND (start <= ?)
            AND relocationaction.end = co2.maxend';

我找到了使用这种表示法的可能解决方案,但它被呈现为一个完全不同且错误的 sql 语句,带有连接和奇怪的表名。

$tbl    = $this->getDbTable();
$select = $tbl->select()->setIntegrityCheck(false);

$subSelect = $select->from('relocationaction', array('vehicle', 'maxend' => 'MAX(relocationaction.end)'))
                    ->group('vehicle');
$subSelectString = '(' . $subSelect->__toString() . ')';

$select ->from(
                array('relocationaction'), array('id', 'date' => 'start', 'enddate' => 'end', 'return'),
                array('co2' => $subSelectString)
            )
        ->joinLeft('exhibitvehicle', 'exhibitvehicle.id = relocationaction.vehicle', array())
        ->where('co2.vehicle = relocationaction.vehicle')
        ->where('relocationaction.monitor = 1')
        ->where('relocationaction.return IS NULL')
        ->where('start <= ?', $start->get('yyyy-MM-dd'))
        ->where('relocationaction.end = co2.maxend');

谁能给我一个提示?

谢谢杰西

更新

这是第二个表达式的结果(总垃圾)

SELECT `relocationaction`.`vehicle`, 
    MAX(relocationaction.end) AS `maxend`, 
    `relocationaction_2`.`id`, 
    `relocationaction_2`.`start` AS `date`, 
    `relocationaction_2`.`end` AS `enddate`, 
    `relocationaction_2`.`return` 
FROM `relocationaction`
INNER JOIN `(
    SELECT ``relocationaction``.``vehicle``,
    MAX(relocationaction.end) AS ``maxend`` FROM ``relocationaction`` GROUP BY ``vehicle``)`.`relocationaction` AS `relocationaction_2`
LEFT JOIN `exhibitvehicle` ON exhibitvehicle.id = relocationaction.vehicle 
WHERE (col2.vehicle = relocationaction.vehicle) 
AND (relocationaction.monitor = 1) 
AND (relocationaction.return IS NULL) 
AND (start <= '2013-05-08') 
AND (relocationaction.end = col2.maxend) 
GROUP BY `vehicle`
4

2 回答 2

1

如果你使用stringin from(),Zend_Db_Select 会认为它是一个表名,所以它会转义它。

解决方案是将您的子选择包装到 Zend_Db_Expr 中。

$tbl    = $this->getDbTable();
$select = $tbl->select()->setIntegrityCheck(false);

$subSelect = $select->from('relocationaction', array('vehicle', 'maxend' => 'MAX(relocationaction.end)'))
                ->group('vehicle');
$subSelectString = '(' . $subSelect->__toString() . ')';

$select ->from(
            array('relocationaction'), array('id', 'date' => 'start', 'enddate' => 'end', 'return'),
            array('co2' => new Zend_Db_Expr($subSelectString))
        )
    ->joinLeft('exhibitvehicle', 'exhibitvehicle.id = relocationaction.vehicle',     array())
    ->where('co2.vehicle = relocationaction.vehicle')
    ->where('relocationaction.monitor = 1')
    ->where('relocationaction.return IS NULL')
    ->where('start <= ?', $start->get('yyyy-MM-dd'))
    ->where('relocationaction.end = co2.maxend');
于 2013-12-12T14:38:40.357 回答
0

好的,我们开始吧。我努力寻找 Zend_Db_Table 的解决方案,但失败了。这就是为什么我最终按照@user466764 的建议使用 PDO 做到了。谢谢你的帮助。

$tbl    = $this->getDbTable();
    $query = 'SELECT relocationaction.id,
                        relocationaction.vehicle,
                        relocationaction.start,
                        relocationaction.end,
                        relocationaction.return
            FROM relocationaction
              (SELECT vehicle, MAX(end) AS maxend
              FROM relocationaction
              GROUP BY vehicle) AS co2
            WHERE co2.vehicle = relocationaction.vehicle
            AND(relocationaction.monitor = 1)
            AND (relocationaction.return IS NULL)
            AND (start <= "' . $start->get('yyyy-MM-dd') . '")
            AND relocationaction.end = co2.maxend';

    $sth = $tbl->getAdapter()->prepare($query);
    $sth->execute();
    $entries = $sth->fetchAll();
于 2013-05-09T08:41:51.427 回答