Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL query date ranges

I have googled but did not find anything related. I have a MySQL table like this:

+++++++++++++++++++++++++++++++
| roomID |    date    | price |
+++++++++++++++++++++++++++++++
|  1     | 2012-10-10 |   10  |
|  1     | 2012-10-11 |   10  |
|  1     | 2012-10-12 |   10  |
|  1     | 2012-10-13 |   12  |
|  2     | 2012-10-10 |   15  |
|  2     | 2012-10-11 |   15  |
|  2     | 2012-10-12 |   15  |
|  2     | 2012-10-13 |   16  |
|  2     | 2012-10-14 |   16  |
|  2     | 2012-10-15 |   16  |
+++++++++++++++++++++++++++++++

I need to get periods based on price and roomID:

++++++++++++++++++++++++++++++++++++++++++++
| roomID |    from    |    till    | price |
++++++++++++++++++++++++++++++++++++++++++++
|    1   | 2012-10-10 | 2012-10-12 |   10  |
|    1   | 2012-10-13 | 2012-10-13 |   12  |
|    2   | 2012-10-10 | 2012-10-12 |   15  |
|    2   | 2012-10-13 | 2012-10-15 |   16  |
++++++++++++++++++++++++++++++++++++++++++++

Thank you!

like image 629
iwtech.ru Avatar asked Nov 23 '25 02:11

iwtech.ru


1 Answers

select roomid,
       min(date) as from,
       max(date) as till,
       price
from periods
group by price
order by price
like image 56
juergen d Avatar answered Nov 28 '25 00:11

juergen d