微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 MySQL 中使用 DATE BETWEEN 检索多行

如何解决在 MySQL 中使用 DATE BETWEEN 检索多行

我有下表结构。

ID   |   FromDate   |    ToDate
1    |  2020-01-02  |  2020-06-01
2    |  2020-08-01  |  2020-12-01
3    |  2020-01-02  |  2020-11-28
4    |  2020-04-01  |  2020-05-28

当我传递两个名为 fromDatetoDate 的输入参数时,它应该选择相关记录。

例如如果 FromDate = 2020-01-01ToDate = 2020-06-01,它应该返回 ID 为 1 和 4 的记录,因为这两个记录在日期之间,作为提供的参数。

我目前正在使用下面的方法,我觉得它浪费了很多资源。

AND ToDate IN (select * from 
(select adddate('2000-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 
union select 7 union select 8 union select 9) t0,(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6                 union select 7 union select 8 union select 9) t1,(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where selected_date between '2021-01-02 00:00:00' and '2022-03-02 00:00:00') ORDER BY name ASC,lt.ID ASC<code>

还有其他方法可以完成这项任务吗?

解决方法

=filter(A2:A,B2:B="ok")

这种查询格式可以获取插入日期之间的记录

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。