SELECT min and maximum value from part of table in MySQL

If I want to select the minimum and maximum values ​​from the whole table, I can use this:

SELECT min(price) as min_price, max(price) as max_price FROM `prices` 

But how to choose the minimum and maximum values ​​from only one part of the table? For example, I have 30 rows in a table. I want to select the min and max values ​​from the first ten lines, then from the second ten lines and then form the last 10.

I tried something like

 SELECT min(price) as min_price, max(price) as max_price FROM `prices` LIMIT 0,10 

but it didn’t work.

How can I solve this problem with minimal queries?

+6
max sql mysql min
source share
2 answers
 SELECT MIN(PRICE) AS MinPrice, MAX(PRICE) AS MaxPrice FROM (SELECT PRICE FROM PRICES LIMIT 10) tmp; 

in addition, MySQL has a cool function that will allow you to return an arbitrary row of rows (for example, return rows 10-20). This is very convenient for displaying post pages:

 SELECT column FROM table LIMIT 10 OFFSET 20 

The above query will return lines 20-30.

So, to return lines from 20 to 30 in case of your request, you use:

 SELECT MIN(PRICE) AS MinPrice, MAX(PRICE) AS MaxPrice FROM (SELECT PRICE FROM PRICES LIMIT 10 OFFSET 20); 

You need to change the offset value to indicate the starting point of your range.

+13
source share

You tried:

 SELECT min(price) as min_price, max(price) as max_price FROM (SELECT price FROM `prices` LIMIT 0,10); 
+6
source share

All Articles