选择“0” AS 变量...与 Zend_Db_Select

发布于 2024-08-23 04:10:29 字数 1134 浏览 4 评论 0原文

我正在尝试创建一个使用以下结构的选择语句:

$db
    ->select()  
    ->from(  
        array('i' => ...),  
        array('provisional', 'itemID', 'orderID'))  
    ->columns(array("'0' AS provisionalQty", "'ballast' AS productType"))  
    ->joinLeft(  
        array('o' => ...),  
        'i.orderID = o.orderID', array())  
    ->joinLeft(  
        array('f' => ...),  
        'i.productID = f.fixtureID AND f.supplierID = o.supplierID', array())  
    ->joinLeft(  
        array('b' => ...),  
        'f.lampTechnology = b.lampTechnology ' .  
        ' AND f.lampCount = b.lampCount ' .  
        ' AND f.ballastVoltage = b.ballastVoltage ' .  
        ' AND b.supplierID = o.supplierID')  
    ->where('i.orderID = ?', $oObj->orderID, Zend_Db::INT_TYPE)  
    ->where('!i.hidden AND i.productType = ? AND !i.provisional', 'fixture')  

MySQL 中的等效项看起来像这样(工作正常)...

SELECT '0' AS provisionalQty, 'ballast' AS productType, i.* FROM ... LEFT JOIN ... WHERE ...;

但是,这并不按预期工作。 $db->columns() 方法期望每个列(甚至是“伪”列)附加一个表。有什么想法吗?

-克里斯

I am trying to create a select statement that uses the following structure:

$db
    ->select()  
    ->from(  
        array('i' => ...),  
        array('provisional', 'itemID', 'orderID'))  
    ->columns(array("'0' AS provisionalQty", "'ballast' AS productType"))  
    ->joinLeft(  
        array('o' => ...),  
        'i.orderID = o.orderID', array())  
    ->joinLeft(  
        array('f' => ...),  
        'i.productID = f.fixtureID AND f.supplierID = o.supplierID', array())  
    ->joinLeft(  
        array('b' => ...),  
        'f.lampTechnology = b.lampTechnology ' .  
        ' AND f.lampCount = b.lampCount ' .  
        ' AND f.ballastVoltage = b.ballastVoltage ' .  
        ' AND b.supplierID = o.supplierID')  
    ->where('i.orderID = ?', $oObj->orderID, Zend_Db::INT_TYPE)  
    ->where('!i.hidden AND i.productType = ? AND !i.provisional', 'fixture')  

The equivalent in MySQL would look something like this (which works fine)...

SELECT '0' AS provisionalQty, 'ballast' AS productType, i.* FROM ... LEFT JOIN ... WHERE ...;

This, however, does not work as expected. The $db->columns() method expects there to be a table attached to each column even the 'pseudo'-columns. Any ideas?

-Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若言繁花未落 2024-08-30 04:10:29

由于此类的工作方式,似乎使用字符串/数组查找特定的表,而 Zend_Db_Expr 类不需要实际的表。

$db
    ->select()
    ->columns(new Zend_Db_Expr("'0' AS provisionalQty, 'ballast' AS productType"))

Because of the way this class works, it seems that using a string/array looks for a specific table, whereas the Zend_Db_Expr class does not require an actual table.

$db
    ->select()
    ->columns(new Zend_Db_Expr("'0' AS provisionalQty, 'ballast' AS productType"))
壹場煙雨 2024-08-30 04:10:29

我认为您在这里错误地使用了列选择。它应该是这样的:

// "ballast AS productType, 0 as provisionalQty"
$db->columns(array("productType" => "ballast", "provisionalQty" => 0));

只要记住列抽象与正常的 AS 语句相反,并且您不必包含“AS”。

I think you're using column select incorrectly here. It should be something like:

// "ballast AS productType, 0 as provisionalQty"
$db->columns(array("productType" => "ballast", "provisionalQty" => 0));

Just remember the columns abstraction is reverse of the normal AS statement and you shouldn't have to include "AS."

似狗非友 2024-08-30 04:10:29

对于那些正在谷歌搜索并寻找 Zend 2Laminas 解决方案的人,这里是我的代码:

$sql = new Laminas\Db\Sql\Sql($this->adapter);
$select = $sql->select();
$select->from(['i' => ...])
    ->columns([
        'id' => 'id',
        'provisionalQty' => new Laminas\Db\Sql\Predicate\Expression('"0"'),
        'productType' => new Laminas\Db\Sql\Predicate\Expression('"ballast"')
    ]);

For those who are googling and looking for Zend 2 or Laminas solution, here is my code:

$sql = new Laminas\Db\Sql\Sql($this->adapter);
$select = $sql->select();
$select->from(['i' => ...])
    ->columns([
        'id' => 'id',
        'provisionalQty' => new Laminas\Db\Sql\Predicate\Expression('"0"'),
        'productType' => new Laminas\Db\Sql\Predicate\Expression('"ballast"')
    ]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文