1

我使用此查询来获取昨天的所有销售额。将其导出到某个地方。
由于有很多已取消,待付款,处理等,我想只导出已完成的。

但不知何故,mysql只是忽略了拳头限制。我一直得到所有的销售。我写什么并不重要。它只需要是昨天使用的状态。

$sql = '

SELECT
        sales_flat_order_grid.entity_id,
        sales_flat_order_grid.increment_id AS increment_id,
        sales_flat_order_grid.quote_id AS quote_id,
        sales_flat_order_grid.store_name AS store_name,
        sales_flat_order_grid.created_at AS created_at,
        sales_flat_order_grid.billing_name AS billing_name,
        sales_flat_order_grid.payment_method AS payment_method,
        sales_flat_order_grid.grand_total AS grand_total,
        sales_order_status.label AS status,
        sales_flat_order_item.sku AS sku,
        sales_flat_order_item.name AS name,
        sales_flat_order_item.price_incl_tax AS price_incl_tax,
        sales_flat_order_item.product_options AS product_options,
        sales_flat_order_payment.additional_information AS additional_information

FROM
        sales_flat_order_grid,
        sales_order_status,
        sales_flat_order_item,
        sales_flat_order_payment

WHERE
        sales_flat_order_grid.status = "Complete"
        AND sales_flat_order_grid.entity_id = sales_flat_order_item.order_id
        AND sales_flat_order_grid.entity_id = sales_flat_order_payment.entity_id
        AND sales_flat_order_grid.created_at >= "' . $date . ' 00:00:00"
        AND sales_flat_order_grid.created_at <= "' . $date . ' 23:59:59"

ORDER BY
        sales_flat_order_grid.entity_id DESC

';
4

3 回答 3

3

当您编写这样的查询时,您正在为灾难做好准备:

FROM
    sales_flat_order_grid,
    sales_order_status,
    sales_flat_order_item,
    sales_flat_order_payment

WHERE
    sales_flat_order_grid.status = "Complete"
    AND sales_flat_order_grid.entity_id = sales_flat_order_item.order_id
    AND sales_flat_order_grid.entity_id = sales_flat_order_payment.entity_id
    AND sales_flat_order_grid.created_at >= "' . $date . ' 00:00:00"
    AND sales_flat_order_grid.created_at <= "' . $date . ' 23:59:59"

为什么?因为很容易错过一个JOIN条件。

相反,请使用以下语法:

FROM
    sales_flat_order_grid
    INNER JOIN sales_order_status ON ....
    INNER JOIN sales_flat_order_item ON sales_flat_order_grid.entity_id = sales_flat_order_item.order_id
    INNER JOIN sales_flat_order_payment ON sales_flat_order_grid.entity_id = sales_flat_order_payment.entity_id

WHERE
    sales_flat_order_grid.status = "Complete"
    AND sales_flat_order_grid.created_at >= "' . $date . ' 00:00:00"
    AND sales_flat_order_grid.created_at <= "' . $date . ' 23:59:59"

看到....那里了吗?它缺少连接条件,因此它选择了笛卡尔积。添加缺少的连接条件,您的结果应该更符合您的期望。

于 2013-01-24T16:41:05.593 回答
1

FROM 子句中有一个名为“sales_order_status”的表,但我看不到它是如何与其他表连接的。所以我认为这就是问题所在。您在 JOIN 子句中缺少与 sales_order_status 表相关的部分。

所以你目前得到的是sales_order_status表和你通过加入sales_flat_order_gridsales_flat_order_itemsales_flat_order_payment得到的结果表之间的交叉连接。因此,您可以在查询结果中看到所有订单状态。

于 2013-01-24T16:36:14.037 回答
1

我认为您 where 的问题是您希望列值同时具有两个不同的值,也许您希望该列具有这些值中的任何一个

这可能有效

WHERE
        sales_flat_order_grid.status = "Complete"
        AND (sales_flat_order_grid.entity_id = sales_flat_order_item.order_id
             OR sales_flat_order_grid.entity_id = sales_flat_order_payment.entity_id)
        AND sales_flat_order_grid.created_at >= "' . $date . ' 00:00:00"
        AND sales_flat_order_grid.created_at <= "' . $date . ' 23:59:59"
于 2013-01-24T16:38:16.513 回答