数据库
首页 > 数据库> > MySQL:开始日期和结束日期之间的正确日期范围为两个字段

MySQL:开始日期和结束日期之间的正确日期范围为两个字段

作者:互联网

我有下表叫做季节

+----+--------+------------+------------+
| id | cost   | start      | end        |
+----+--------+------------+------------+
| 33 |    255 | 2014-01-05 | 2014-04-16 |
| 17 |    357 | 2014-04-17 | 2014-04-19 |
| 65 | 191.25 | 2014-04-20 | 2014-07-10 |
| 49 |    255 | 2014-07-11 | 2014-08-23 |
| 81 | 191.25 | 2014-08-24 | 2014-12-18 |
+----+--------+------------+------------+

我试图使用以下查询获得开始和结束之间的日期范围.

SELECT 
    *
FROM
    seasons
WHERE
    (start BETWEEN '2014-01-05' AND '2014-01-05' OR end BETWEEN '2014-01-05' AND '2014-01-05');

如果开始日期恰好在字段上的值上开始,我就能得到结果集.

+----+------+------------+------------+
| id | cost | start      | end        |
+----+------+------------+------------+
| 33 |  255 | 2014-01-05 | 2014-04-16 |
+----+------+------------+------------+

现在的问题是我将日期提前到

2014-01-06

SELECT 
    *
FROM
    seasons
WHERE
    (start BETWEEN '2014-01-06' AND '2014-01-06' OR end BETWEEN '2014-01-06' AND '2014-01-06');

Empty set (0.00 sec)

没有结果.如何获取SQL之间不同字段的日期范围?

任何帮助表示赞赏.

解决方法:

你的逻辑落后了.如果您想要在该季节中给定日期的季节,那么您的where子句应如下所示:

WHERE '2014-01-06' BETWEEN start AND end;

标签:date-range,between,mysql
来源: https://codeday.me/bug/20190725/1531507.html