I use this query to get all sales from yesterday. To export this somewhere.
Since there are a lot of cancelled, pending payment, processing and so on, I thought to export only the ones that are completed.
But somehow mysql just ignoes the fist where restriction.
I get all the sales all the time. It doesn’t matter what i write in.
It only has to be a state that was used yesterday.
$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
';
You’re setting yourself up for disaster when you write queries like this:
Why? Because it’s too easy to miss a
JOINcondition.Instead, use this syntax:
See the
....there? It’s missing a join condition and therefore it selects a cartesian product instead. Add the missing join condition and your results should be more in line with your expectations.