K
K
koliane2019-10-19 16:23:09
MySQL
koliane, 2019-10-19 16:23:09

How to make the following request?

There are two tables:
1) 6840193ee0.jpg
contains, for example, a certain price
and 2) 98edffefe5.jpg
contains the id of the price ranges from the first table. That is, id_1 is the id of the beginning of the range, and id_2 is the rightmost id of the range.
Tell me how to make a query in which we will get a selection with the maximum prices of each of the ranges?
Ie, for our case, the sample should be as follows:
0ab7c0f28c.jpg
Example of counting for a range with id_1=4 and id_2=7:
I.e. you need to find the maximum price between these id.
id=4 price 20
id=5 price 5
id=6 price 3
id=7 price 100
Maximum price from this range = 100

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2019-10-19
@koliane

GREATEST( prices1.value, prices2.value )

SELECT ID_1, ID_2,
  ( SELECT MAX( Value ) FROM Prices WHERE ID BETWEEN ID_1 AND ID_2 ) AS PriceMax
  FROM Range

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question