mysql 年月日不同字段 如何按照区间筛选

mysql 年月日不同字段 如何按照区间筛选
如题 有趣的题 如何快速查询且准确


eg:

   id   month  day
    1     5     1
    2     5     6  
    3     6     19
    4     7     1
    5     7     2

筛选出5月6号-7月1号的id

阅读 3.9k
5 个回答

mysql有字符串转时间和时间戳的函数 也有将多个字段拼接的函数
拼接下转下格式 再比对区间参数

新加一列, 保存成date, 在date列上建立索引

通用开始,中间连续,结束三段union

select id from tbl where month = 5 and day >= 6
union 
select id from tbl where month > 5 and month < 7
union 
select id from tbl where month = 7 and day <= 1

针对特殊日期

select id from tbl where month = 5 and day >= 6
union 
select id from tbl where month = 6
union 
select id from tbl where month = 7 and day = 1

上面的 sql 语句加组合索引

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏