如何使用左连接

时间:2017-02-15 12:08:34

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

我已经编写了一个代码来从表中检索客户的城市,电子邮件,街道和其他详细信息order_address这里的问题是每个订单有两行,其中address_type是{shipping 1}}而另一个是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');
        }

1 个答案:

答案 0 :(得分: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');
        }