0

Hi my table structure is like this

year  month  name
2015   1     adc
2015   2     adc
2015   4     adc
2015   5     adc
2015   6     adc
2015   7     adc
2015   8     adc
2015   9     adc
2015   10    adc
2015   11    adc
2015   12    adc
2016   1     adc
2016   2     adc
2016   3     adc

I want to sort my query result always starting from month 4 of the year and end with month 3 of next year.for example if I want data of Year 2015 it should be like

    2015   4     adc
    2015   5     adc
    2015   6     adc
    2015   7     adc
    2015   8     adc
    2015   9     adc
    2015   10    adc
    2015   11    adc
    2015   12    adc
    2016   1     adc
    2016   2     adc
    2016   3     adc

can i do this with SQL query? here i need to increase the year also after month 12

sanu
  • 1,048
  • 3
  • 14
  • 28
  • 2
    Possible duplicate of [How to define a custom ORDER BY order in mySQL](http://stackoverflow.com/questions/9378613/how-to-define-a-custom-order-by-order-in-mysql) – Alex K. Apr 02 '16 at 17:02

2 Answers2

1

You can combine multiple columns for a condition:

select *
from my_table t
where (year, month) >= (2015,   4)
  and (year, month) <= (2015+1, 3)
order by year, month

This one (in theory) should use the (year, month) index.

Paul Spiegel
  • 30,925
  • 5
  • 44
  • 53
0

solved it, thanks

select *
from my_table t
where (year=$year and month>=4)
  or(year=$year+1 and month<=3)
order by year, month
sanu
  • 1,048
  • 3
  • 14
  • 28
  • Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - [From Review](/review/low-quality-posts/11877629) – semirturgay Apr 03 '16 at 19:26
  • yes that is a complete answer, that actually is the solution – sanu Apr 04 '16 at 11:27
  • If possible, keep your code flexible for requirement changes. What will you do, if you want the entries from 04/2013 to 03/2016? – Paul Spiegel Apr 04 '16 at 19:11
  • 04/2013 to 03/2016 yes this will not work but in my case the year increment is only by 1. I want to sort my query result starting from month 4 of the year and end with month 3 of next year @PaulSpiegel – sanu Apr 05 '16 at 01:11