如何检索邮政编码'来自使用SQL的单独表

时间:2016-10-25 09:49:32

标签: mysql magento magento-1.9

目标

sales_flat_order 表中提取订单详细信息,并在Magento 1.9中的 sales_flat_order_address 表中添加订单的邮政编码

到目前为止

我的查询(正常工作)

SELECT `increment_id` AS 'Order Number', SUM(ROUND(`base_grand_total`,2)) AS 'Total Value', created_at AS 'Order Date', customer_firstname AS 'First Name', customer_lastname AS 'Surname'
FROM `sales_flat_order`
WHERE `shipping_description` LIKE '%next day%'
AND `created_at` BETWEEN '2015-11-01' AND '2015-11-05'
GROUP BY `increment_id`
ORDER BY `created_at` DESC

到目前为止的结果

订单号|总价值|订单日期|名字|姓

问题

正如您所看到的,我可以从 sales_flat_order 中提取数据,但很难从 sales_flat_order_address 中提取数据。

解决方案

以下查询已返回我的结果,但我觉得效率不高。如果有更有效的方式,请告诉我。感谢

SELECT `increment_id` AS 'Order Number', SUM(ROUND(`base_grand_total`,2)) AS 'Total Value', created_at AS 'Order Date', customer_firstname AS 'First Name', customer_lastname AS 'Surname', `postcode` AS 'Postcode'
FROM `sales_flat_order`, `sales_flat_order_address`
WHERE `shipping_description` LIKE '%next day%'
AND sales_flat_order.billing_address_id = sales_flat_order_address.entity_id
AND `created_at` BETWEEN '2015-11-01' AND '2015-11-05'
GROUP BY `increment_id`
ORDER BY `created_at` DESC

1 个答案:

答案 0 :(得分:0)

现在首选的语法是使用JOIN .... ON,但据我所知,效率没有差别。

SELECT `increment_id` AS 'Order Number', SUM(ROUND(`base_grand_total`,2)) AS 'Total Value', created_at AS 'Order Date', customer_firstname AS 'First Name', customer_lastname AS 'Surname', `postcode` AS 'Postcode'
FROM `sales_flat_order`
JOIN `sales_flat_order_address` ON sales_flat_order.billing_address_id = sales_flat_order_address.entity_id
WHERE `shipping_description` LIKE '%next day%'
AND `created_at` BETWEEN '2015-11-01' AND '2015-11-05'
GROUP BY `increment_id`
ORDER BY `created_at` DESC