这是失败的查询:
SELECT * FROM court_records
WHERE county = 1
AND last_exported_date > '2017-04-21 00:00:00 -0400' AND last_exported_date <= '2017-04-21 23:59:59 -0400';
Empty set, 1 warning (0.12 sec)
它说找不到任何记录。但后来我知道今天的last_exported_date有很多记录:
SELECT COUNT(*) FROM court_records WHERE DATE(last_exported_date) = CURDATE();
+----------+
| COUNT(*) |
+----------+
| 1609 |
+----------+
事实上,这只是一个例子:
SELECT * FROM court_records WHERE id = 43862\G;
*************************** 1. row ***************************
id: 43862
county: 1
last_exported_date: 2017-04-21
created_at: 2017-04-21 16:24:43
updated_at: 2017-04-21 17:12:58
那么为什么我的第一个查询返回一个空集?我该如何解决?
答案 0 :(得分:0)
你可以试试这个
WHERE last_exported_date >= '2017-04-21' AND startTime < ('2017-04-21' + INTERVAL 1 DAY)
或者您可以像这样查看日期
WHERE DATE(last_exported_date)='2017-04-21'
答案 1 :(得分:0)
您的日期范围构造不正确。
你有
last_exported_date > '2017-04-21 00:00:00 -0400' /* off-by-one! */
AND last_exported_date <= '2017-04-21 23:59:59 -0400';
要获取2017年4月21日的日期记录,您需要此...请注意对>=
和<
的更改
last_exported_date >= '2017-04-21 00:00:00 -0400'
AND last_exported_date < '2017-04-22 00:00:00 -0400';
第一个表达式(问题中的一个)恰好排除DATE
值为2017-04-21
或DATETIME
值为2017-04-21 00:00:00
的所有记录。
而且,在查找一整天的记录时,使用< the next day
比<= the last moment of the current day
更清洁。
顺便说一句,你的时区指标在MySQL中没有任何用处。