D
D
Dima Pautov2016-01-29 18:24:59
MySQL
Dima Pautov, 2016-01-29 18:24:59

How can a query be optimized?

The task is as follows:
Get the types and names of products for the manufacture of which the most expensive parts are used;
There are 3 tables:
items [`id`, `view`, `name`, `description`] - Stores items
details [`id`, `name`, `price`, `unit_detail`] - Stores the parts that are used to make items
details_in_items [`id_item`, `id_detail`, `count`] - Stores the details used in item
[`id_item`,`id_detail`] in details_in_items table linked by primary key
My query is this:

SELECT view,name 
FROM `items` WHERE id IN (SELECT id_item FROM  details_in_items WHERE id_detail IN (SELECT id FROM `details` WHERE price=(SELECT MAX(price) FROM details)))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksej, 2016-01-29
@Shwed_Berlin

SELECT item.view, item.name, item.description
FROM items AS item
INNER JOIN details_in_items AS cross ON item.id = cross.id_item 
            AND cross.id_detail in (SELECT TOP 5 id FROM details ORDER BY price DESC)

will select all products that use the 5 (TOP 5) most expensive parts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question