Forum Moderators: open

Message Too Old, No Replies

MySQL - Set Rows for Each Product ID

         

Redshot

11:08 am on Aug 14, 2014 (gmt 0)

10+ Year Member



I am retrieving product records / sales order from a database. I am joining table results using MySQL's **UNION** feature.

First of all this is the query that i used:

SET @meta_id=0;
SELECT @meta_id:= @meta_id+1 AS meta_id, order_item_id, meta_key, meta_value FROM (
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_qty' AS meta_key, 1 AS meta_value, 1 AS origin
FROM bso
WHERE bso.ID IN (832)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_tax_class' AS meta_key, '' AS meta_value, 2 AS origin
FROM bso
WHERE bso.ID IN(832)
UNION
SELECT bsb.ORDER_ID AS meta_id, bsb.ORDER_ID AS order_item_id, '_product_id' AS meta_key, bsb.PRODUCT_ID AS meta_value, 3 AS origin
FROM bsb
WHERE bsb.ORDER_id IN (832)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_variation_id' AS meta_key, '' AS meta_value, 4 AS origin
FROM bso
WHERE bso.ID IN (832)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_line_subtotal' AS meta_key, '' AS meta_value, 5 AS origin
FROM bso
WHERE bso.ID IN (832)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_line_total' AS meta_key, '' AS meta_value, 6 AS origin
FROM bso
WHERE bso.ID IN (832)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_line_tax' AS meta_key, 0 AS meta_value, 7 AS origin
FROM bso
WHERE bso.ID IN (832)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_line_subtotal_tax' AS meta_key, 0 AS meta_value, 8 AS origin
FROM bso
WHERE bso.ID IN (832)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, 'method_id' AS meta_key, bsd.NAME AS meta_value, 9 AS origin
FROM bso
LEFT OUTER JOIN bsd ON bso.DELIVERY_ID = bsd.ID
WHERE bso.ID IN (832)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, 'cost' AS meta_key, bsd.PRICE AS meta_value, 10 AS origin
FROM bso
LEFT OUTER JOIN bsd ON bso.DELIVERY_ID = bsd.ID
WHERE bso.ID IN (832)
) sales_order_meta ORDER BY order_item_id, meta_id

If I set my query to retrieve only 1 product the order of **ROWS** are OK. Attached is a screenshot link:
[i.stack.imgur.com...] - This **sales order** only has **1 product bought**

This time I want to **retrieve** a **sales order** with **multiple products** and I want to
**arrange** the **results of each product_id** in a way that it is **like** the **first link** .. This is the result of the query above whenever there is a sales order with multiple products:
[i.stack.imgur.com...]

Note: Since **order_item_id** has **4** products ordered. I desire them to be arranged like this the first link so that each product has its own _qty, _tax_class, _product_id, _variation_id, _line_subtotal, _line_total, _line_tax, _line_subtotal_tax, method_id, cost

I want them to look like this: [clip2net.com...]

Kindly Please help Me.