Phalcon ExecuteQuery内部加入问题

时间:2016-07-19 13:23:55

标签: phalcon

这是Native SQL Query,它在MySql中工作:

SELECT a.pshdQty
FROM production_stock_header a
INNER JOIN (SELECT MAX(pshdId) AS pshdId
FROM production_stock_header
WHERE pshdAuditDelete = 'N'
) b
ON a.pshdId = b.pshdId

但是,为什么在Phalcon这总是错误?

$triggerSet = $this->modelsManager->executeQuery("
                    SELECT  pshdQty
                    FROM ProductionStockHeader  
                    INNER JOIN (SELECT MAX(pshdId) AS pshdId
                    FROM ProductionStockHeader
                    WHERE ProductionStockHeader.AuditDelete = 'N'
                    ) AS b
                    ON ProductionStockHeader.pshdId = b.pshdId
                ");

1 个答案:

答案 0 :(得分:1)

Phalcon ORM没有提供编写此类join语句的方法 我建议您在数据库连接上运行原始本机SQL 查询:

$triggerSet = $this->db->query("SELECT a.pshdQty
        FROM production_stock_header a
        INNER JOIN (SELECT MAX(pshdId) AS pshdId
            FROM production_stock_header
            WHERE pshdAuditDelete = 'N'
        ) b
        ON a.pshdId = b.pshdId")->fetch();

$this->db指向您在服务中注册的数据库连接。

您也可以将内部联接放在一起:

$triggerSet = $this->modelsManager->executeQuery("
    SELECT pshdQty
    FROM ProductionStockHeader
    WHERE AuditDelete = 'N'
    ORDER BY pshdId DESC
    LIMIT 1
");