mysql忽略where语句

时间:2013-01-24 16:29:22

标签: php mysql debian magento-1.7

我使用此查询从昨天获得所有销售。把它出口到哪里。
由于有很多取消,待付款,处理等,我想只导出已完成的那些。

但不知何故,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

';

3 个答案:

答案 0 :(得分: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"

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

答案 1 :(得分:1)

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

因此,您目前获得的是 sales_order_status 表与加入 sales_flat_order_grid sales_flat_order_item后得到的结果表交叉加入 sales_flat_order_payment 。因此,您会在查询结果中看到所有订单状态。

答案 2 :(得分:1)

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

这可能有效

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"