D
D
danforth2016-04-05 10:56:24
PHP
danforth, 2016-04-05 10:56:24

How to make two simple queries to the database?

Hello.
There are tables: shop_order and shop_order_items
In shop_order I need two values: discount and currency
In shop_order_items I need two values: price and purchase_price.
All this data is displayed for one order. It turns out I need to do this:
SELECT `discount`, `currency` FROM `shop_order` WHERE `id`=1;
SELECT `price`, `purchase_price` FROM `shop_order_items` WHERE `order_id`=1;
From the point of view of optimization and good practice, can these queries be somehow combined to speed up or make it more correct?
I tried to pour everything into one SELECT, but then it turns out that the values ​​begin to be duplicated, i.e. for one product - two rows.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Ivashkevich, 2016-04-05
@danforth

Use JOINs.
For example

SELECT o.discount, o.currency, i.price, i.purchase 
FROM shop_order o 
RIGHT JOIN shop_order_items i
WHERE o.id=i.order_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question