我正在尝试从数据库中获取最近14天的记录,以及未来14天的记录。过去我可以得到,但未来是一个问题。
以下是我正在尝试的约会格式(8天样本)的示例,该格式在我的服务器上正常运行:
mysql> select DATE_SUB(CURDATE(),INTERVAL +8 DAY);
+-------------------------------------+
| DATE_SUB(CURDATE(),INTERVAL +8 DAY) |
+-------------------------------------+
| 2017-09-07 |
+-------------------------------------+
1 row in set (0.00 sec)
mysql> select DATE_SUB(CURDATE(),INTERVAL -8 DAY);
+-------------------------------------+
| DATE_SUB(CURDATE(),INTERVAL -8 DAY) |
+-------------------------------------+
| 2017-09-23 |
+-------------------------------------+
1 row in set (0.00 sec)
这似乎可以很好地获取过去和未来的日期,但是,当我尝试使用我的实际查询进行内联时,它不希望为将来的约会工作。
mysql> SELECT estatu,edates,etimes
FROM tevent
WHERE edates
BETWEEN DATE_SUB(CURDATE(),INTERVAL +8 DAY) AND CURDATE();
+----------+------------+--------+
| estatu | edates | etimes |
+----------+------------+--------+
| Upcoming | 2017-09-15 | 19:00 |
| Upcoming | 2017-09-15 | 17:00 |
| Results | 2017-09-09 | 15:00 |
| Upcoming | 2017-09-15 | |
| Results | 2017-09-08 | |
| Results | 2017-09-08 | 19:00 |
| Results | 2017-09-08 | 19:30 |
| Results | 2017-09-08 | 19:30 |
| Results | 2017-09-09 | 09:30 |
| Results | 2017-09-09 | 12:00 |
| Results | 2017-09-09 | 15:00 |
| Results | 2017-09-09 | 17:00 |
| Results | 2017-09-09 | 18:00 |
| Results | 2017-09-09 | 19:30 |
| Results | 2017-09-09 | 20:30 |
| Upcoming | 2017-09-15 | |
+----------+------------+--------+
16 rows in set (0.00 sec)
mysql> SELECT estatu,edates,etimes
FROM tevent
WHERE edates
BETWEEN DATE_SUB(CURDATE(),INTERVAL -8 DAY) AND CURDATE();
Empty set (0.00 sec)
这是我到目前为止最接近尝试在网上和网上其他地方找到的各种脚本。
此查询只是为了确认我有未来事件:
mysql> SELECT estatu,edates,etimes
FROM tevent
WHERE edates
LIMIT 10;
+----------+------------+--------+
| estatu | edates | etimes |
+----------+------------+--------+
| Upcoming | 2017-09-15 | 19:00 |
| Upcoming | 2017-09-15 | 17:00 |
| Upcoming | 2017-09-16 | 17:00 |
| Upcoming | 2017-09-23 | 15:00 |
| Upcoming | 2017-09-23 | 17:00 |
| Results | 2017-09-09 | 15:00 |
| Upcoming | 2017-09-15 | |
| Results | 2017-03-26 | |
| Results | 2017-04-02 | |
| Results | 2017-04-09 | |
+----------+------------+--------+
10 rows in set (0.00 sec)
编辑:我需要两个单独的结果,一个用于未来事件,一个用于过去事件。每个都将独立于单独的PHP页面连接请求。
======
感谢@etsa发现问题的格式。以下两个查询提供了所需的内容,同样是8天样本:
SELECT estatu,edates,etimes FROM tevent WHERE edates BETWEEN DATE_SUB(CURDATE(),INTERVAL +8 DAY) AND CURDATE();
SELECT estatu,edates,etimes FROM tevent WHERE edates BETWEEN CURDATE() AND DATE_SUB(CURDATE(),INTERVAL -8 DAY);
结果如下:
mysql> SELECT estatu,edates,etimes FROM tevent WHERE edates BETWEEN DATE_SUB(CURDATE(),INTERVAL +8 DAY) AND CURDATE();
+----------+------------+--------+
| estatu | edates | etimes |
+----------+------------+--------+
| Upcoming | 2017-09-15 | 19:00 |
| Upcoming | 2017-09-15 | 17:00 |
| Results | 2017-09-09 | 15:00 |
| Upcoming | 2017-09-15 | |
| Results | 2017-09-08 | |
| Results | 2017-09-08 | 19:00 |
| Results | 2017-09-08 | 19:30 |
| Results | 2017-09-08 | 19:30 |
| Results | 2017-09-09 | 09:30 |
| Results | 2017-09-09 | 12:00 |
| Results | 2017-09-09 | 15:00 |
| Results | 2017-09-09 | 17:00 |
| Results | 2017-09-09 | 18:00 |
| Results | 2017-09-09 | 19:30 |
| Results | 2017-09-09 | 20:30 |
| Upcoming | 2017-09-15 | |
+----------+------------+--------+
16 rows in set (0.00 sec)
mysql> SELECT estatu,edates,etimes FROM tevent WHERE edates BETWEEN CURDATE() AND DATE_SUB(CURDATE(),INTERVAL -8 DAY);
+----------+------------+--------+
| estatu | edates | etimes |
+----------+------------+--------+
| Upcoming | 2017-09-15 | 19:00 |
| Upcoming | 2017-09-15 | 17:00 |
| Upcoming | 2017-09-16 | 17:00 |
| Upcoming | 2017-09-23 | 15:00 |
| Upcoming | 2017-09-23 | 17:00 |
| Upcoming | 2017-09-15 | |
| Upcoming | 2017-09-15 | |
| Upcoming | 2017-09-16 | 19:30 |
| Upcoming | 2017-09-16 | 09:30 |
| Upcoming | 2017-09-16 | 19:30 |
| Upcoming | 2017-09-16 | 14:00 |
| Upcoming | 2017-09-16 | 15:00 |
| Upcoming | 2017-09-16 | 19:30 |
| Upcoming | 2017-09-17 | |
| Upcoming | 2017-09-17 | |
+----------+------------+--------+
15 rows in set (0.00 sec)
答案 0 :(得分:1)
由于你的介于两者之间不合适,你没有得到未来的日期:
SELECT estatu,edates,etimes
FROM tevent
WHERE edates
BETWEEN DATE_ADD(CURDATE(),INTERVAL -8 DAY) AND DATE_ADD(CURDATE(),INTERVAL +8 DAY);
在between子句中,first(较早)值为first,较高值为second。
答案 1 :(得分:0)
尝试:
SELECT estatu,edates,etimes
FROM tevent
WHERE edates
BETWEEN CURDATE() AND DATE_SUB(CURDATE(),INTERVAL -8 DAY);
答案 2 :(得分:0)
其他答案都不能满足您的要求,但确实如此:
SELECT
estatu,
edates,
etimes
FROM tevent
WHERE
edates BETWEEN DATE_SUB(CURDATE(),INTERVAL 14 DAY) AND
DATE_ADD(CURDATE(),INTERVAL 14 DAY);
如果您想要过去和现在的单独查询,可以使用:
SELECT estatu, edates, etimes
FROM tevent
WHERE edates BETWEEN DATE_SUB(CURDATE(),INTERVAL 14 DAY) AND CURDATE()
过去和未来:
SELECT estatu, edates, etimes
FROM tevent
WHERE edates BETWEEN CURDATE() AND DATE_ADD(CURDATE(),INTERVAL 14 DAY)