2013-01-24 181 views
1

我使用此查詢從昨天獲得所有銷售。將其導出到某處。
由於有很多取消,待付款,處理等,我以爲只導出已完成的。mysql忽略哪裏聲明

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

'; 
+0

這應該完全正常 - 你確定你的問題不在其他地方嗎?你有沒有嘗試用''''s和相反的方法來切換''''? – h2ooooooo

+0

不,我會試試 –

+0

看不到你有問題的原因,但你似乎有一個無意的交叉連接(沒有JOIN條件指定加入到sakes_order_status) – Kickstart

回答

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" 

....呢?它缺少連接條件,因此它會選擇笛卡爾產品。添加缺失的連接條件,結果應該更符合您的期望。

+0

謝謝對於傑克的建議,我將從現在開始使用連接。 –

1

在FROM子句中有一個名爲「sales_order_status」的表,但我沒有看到它如何與其他表連接。所以我認爲這是問題所在。您在JOIN子句中缺少與sales_order_status表相關的部分。

那麼你得到的是目前一個交叉聯接sales_order_status表,您可以通過加入sales_flat_order_gridsales_flat_order_itemsales_flat_order_payment得到了結果表之間。因此,您可以在查詢結果中看到所有訂單狀態。

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" 
+0

我認爲我們2有同樣的想法,但它不是正確的方式 –

+0

@derty你是對的,我完全錯過了他使用Joins的事實:) – perrohunter