[英]sql query sorting year/month
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 我想对查询结果始终从该年的第4个月开始到下一年的第3个月进行排序。例如,如果我想要2015年的数据,它应该像
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? 我可以使用SQL查询吗? here i need to increase the year also after month 12
在这里,我还需要在第12个月之后增加年份
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. 这个(理论上)应该使用(年,月)索引。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.