A表(字段):年度、期间
B表(字段):年度、期间
这时如果要依据,A/B两个表的时间范围做查询
--错误示例:
where Fyear between Ayear and Byear
and Fperiod between Aperiod and Bperiod
这样为什么会错,举例:
A表(字段):年度、期间
2020 | 2
2020 | 3
2021 | 2
2021 | 3
B表(字段):年度、期间
2020 | 5
2020 | 7
2021 | 5
2021 | 7
错误示例:
where Fyear between 2020 and 2021
and Fperiod between 3 and 7
【错误原因】这样查不出A表的2021年2月的这条数据,因为它不在期间3~7范围内
- 正确示例:
where Fyear*100+Fperiod between Ayear*100+Aperiod and Byear*100+Bperiod
这样相当于写成了“年月”的格式,即:
A表(字段):年度、期间
202002
202003
202102
202103
B表(字段):年度、期间
202005
202007
202105
202107
而这样比较数时,就相当于按完整的数值大小去比较了,即不会有遗漏。
链接:SQL–比较月份
https://blog.csdn.net/Mimo_YY/article/details/118707446