【问题标题】:How to add a where clause with the left join如何使用左连接添加 where 子句
【发布时间】:2017-07-04 02:11:27
【问题描述】:

我编写了一个代码来从表 order_address 中检索客户的城市、电子邮件、街道和其他详细信息,这里的问题是每个订单都有两行,其中一行 address_typeshipping 和另一个是billing。当我查询时,系统会自动从计费的第二行中检索数据。我想要地址类型为运输的行的数据。怎样才能做到这一点?谁能让我知道如何使用此处的 where 子句仅使用 address_type="shipping" 过滤行。

$select = $collection->getSelect();
            $select->joinLeft(array('payment' => $collection->getTable('sales/order_payment')), 'payment.parent_id=main_table.entity_id', array('payment_method' => 'method'));
            $select->joinLeft(array('email' => $collection->getTable('sales/order_address')), 'email.parent_id=main_table.entity_id', array('customer_email' => 'email','customer_phone' => 'telephone','shipping_street' => 'street','shipping_city' => 'city','shipping_postcode' => 'postcode','shipping_region' => 'region','shipping_country' => 'country_id'));
            $select->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id', array('skus' => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ", ")')));
            $select->group('main_table.entity_id');
        }

【问题讨论】:

    标签: sql magento filter left-join where-clause


    【解决方案1】:

    试试下面的代码,希望对你有帮助。我刚刚在第 3 行将 email.address_type=shipping 添加到您的代码中。

    $select = $collection->getSelect();
                $select->joinLeft(array('payment' => $collection->getTable('sales/order_payment')), 'payment.parent_id=main_table.entity_id', array('payment_method' => 'method'));
                $select->joinLeft(array('email' => $collection->getTable('sales/order_address')), 'email.parent_id=main_table.entity_id AND email.address_type="shipping"', array('customer_email' => 'email','customer_phone' => 'telephone','shipping_street' => 'street','shipping_city' => 'city','shipping_postcode' => 'postcode','shipping_region' => 'region','shipping_country' => 'country_id'));
                $select->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id', array('skus' => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ", ")')));
                $select->group('main_table.entity_id');
            }
    

    【讨论】:

    • 这为我获取了我正在寻找的数据。谢谢你:)
    • 最欢迎@AkshayVasu
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多