1

I have an sql statement that has left join between table orders and order_item

$sql = "SELECT * FROM orders 
LEFT JOIN order_item on orders.order_id = order_item.order_id 
LEFT JOIN product on order_item.product_id = product.product_id 
LEFT JOIN categories on categories.categories_id = product.categories_id 
LEFT JOIN brands on brands.brand_id = product.brand_id 
WHERE order_date >= '$start_date' AND order_date <= '$end_date' and order_status = 1";

$query = $connect->query($sql);

I echo $row['quantity']

But it's displaying quantity from table orders and not from table order_item.

How can I echo quantity from order_item?

1 Answer 1

2

You should add the quantity field from order_item into the SELECT. Also, you should use a Column Alias, in order to prevent duplicated column names issues.

This query should do the job:

$sql = "SELECT orders.*, order_item.quantity AS item_quantity FROM orders 
LEFT JOIN order_item on orders.order_id = order_item.order_id 
LEFT JOIN product on order_item.product_id = product.product_id 
LEFT JOIN categories on categories.categories_id = product.categories_id 
LEFT JOIN brands on brands.brand_id = product.brand_id 
WHERE order_date >= '$start_date' AND order_date <= '$end_date' and order_status = 1";

As we are using an alias, you must change the following: $row['quantity'] -> $row['item_quantity']

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

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.