Phalcon ExecuteQuery内部加入问题

问题描述 投票:0回答:1

这是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
                ");
phalcon
1个回答
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
");
© www.soinside.com 2019 - 2024. All rights reserved.