0

I would like to, in one query select orders and have their items attached to them (right now I'm selecting the orders, then using a separate query selecting the order_items - this is proving very slow on a large amount of orders...

orders: id | name | total

order_items: id | order_id | price | qty

order_items_info: id | order_id | order_item_id | tracking_no

The last thing I want to do is: add my order_items_info table to the item array.

$orders = array(
    array(
        'id' => '',
        'name' => '',
        'items' => array(
            array(
                'order_item_id' => '',
                'price' => '',
                'qty' => '',
                'item_info' => array()
            ),
            array(
                'order_item_id' => '',
                'price' => '',
                'qty' => '',
                'item_info' => array()
            ),
            ...
        )
    )
);
3
  • 1
    What's wrong with JOIN? Commented Dec 10, 2013 at 20:30
  • Your order_items table needs an item_id field that joins to your item table. Commented Dec 10, 2013 at 20:32
  • It has one, but it made it a bit complicated so I left it out. Commented Dec 10, 2013 at 22:03

2 Answers 2

1
SELECT o.id,name,total,item_info,price,qty FROM orders o 
JOIN order_items oi ON o.id=oi.order_id
JOIN order_items_info oii ON oii.order_id=o.id
AND oii.order_item_id=oi.id 

Just a wild guess until you post your table info.

Sign up to request clarification or add additional context in comments.

3 Comments

I forgot to add the 2nd part of the question: adding order_items_info to the items array?
I dont see anything order_items_info anywhere in your tables.
Sorry, it was a bit confusing. I've updated it to make a little more sense.
0

select * from orders join order_items on (orders.id = order_id)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.