I have 4 tables.
table_orders
order_id customer_id order_datetime order_payment_type order_delivery_date delivery_time_slot
table_order_details
order_id product_id varient_id quantity product_mrp product_sell_price product_name
table_order_status
order_id status_id status_datetime
table_order_status_values
value_id value_desc
I want to get the order ID, order amount, order date, order quantity, order time, desc_value.
I fulfill this request.
SELECT oo.order_id, oo.amount, oo.date, oo.quantity, oo.time, value_desc FROM ( SELECT s.order_id, SUM(OD.product_sell_price * OD.quantity) as amount, DATE_FORMAT(o.order_datetime, '%d/%m/%Y') as date, SUM(OD.quantity) as quantity, TIME(o.order_datetime) as time, MAX( status_id ) as laststatus FROM table_orders o INNER JOIN table_order_details AS OD ON o.order_id = OD.order_id INNER JOIN table_order_status s ON s.order_id = o.order_id GROUP BY o.order_id )oo INNER JOIN table_order_status_values ON value_id = laststatus order by order_id DESC
Sample data:
Table_orders
1 1 2015:12:12:19:42:47 1 2015:12:14 1
table_order_details
1 12 3 1 21.00 20.00 abcd 1 13 2 2 100.00 90.00 efgh
table_order_status
1 1 2015:12:12:19:42:47 1 2 2015:12:12:20:42:47
table_order_status_values
1 NEW ORDER 2 CONFIRM 3 Delivered
With the above query Conclusion:
1 400.00 12:12:2015 6 19:42:47 CONFIRM
But the expected result:
1 200.00 12:12:2015 3 19:42:47 CONFIRM
I get the order quantity and the number of orders twice (or three times) depending on the number of statuses for this order.
How to fix it? Any help would be greatly appreciated.