我有下面的季节表:
| id | name | start_month | end_month |
------------------------------------------
| 101 | Summer | 12 | 2 |
| 102 | Winter | 6 | 8 |
| 103 | Spring | 9 | 11 |
| 104 | Fall | 3 | 5 |我需要一个月一个季度的数据。假设当前月份是2(2月),我希望Summer作为输出。
我可以通过简单地使用where条件start_month >= 4 and end_month <= 4来让其他季节工作。但这对Summer是行不通的,因为这个季节将跨过明年。
我要怎么处理Summer的案子?
我认为一种解决方案是使用日期,而不是像1980-12-01那样的月份数字,并使用between函数,但对于用户端来说,这变得有点复杂。
如果它可以只使用月份数字,那就太好了。
发布于 2021-02-22 16:48:40
你可以这样做:
(month(d) between start_month and end_month) or
(start_month>end_month and (month(d)>=start_month or month(d)<=end_month))请参阅db-fiddle
https://stackoverflow.com/questions/66312180
复制相似问题