如何使用左连接添加 where 子句
How to add a where clause with the left join
我写了一个代码来从 table order_address 中检索客户的城市、电子邮件、街道和其他详细信息这里的问题是每个订单都有两行,其中一行address_type
行是 shipping
,另一行是 billing
。当我查询时,系统会自动从正在计费的第二行检索数据。我想要地址类型为 shipping 的行的数据。怎样才能做到这一点?谁能告诉我如何使用此处的 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');
}
试试下面的代码希望这有帮助。我刚刚在第 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');
}
我写了一个代码来从 table order_address 中检索客户的城市、电子邮件、街道和其他详细信息这里的问题是每个订单都有两行,其中一行address_type
行是 shipping
,另一行是 billing
。当我查询时,系统会自动从正在计费的第二行检索数据。我想要地址类型为 shipping 的行的数据。怎样才能做到这一点?谁能告诉我如何使用此处的 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');
}
试试下面的代码希望这有帮助。我刚刚在第 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');
}