我有一个这样的查询:
SELECT * FROM bcl1_logs where created_at between '2015-02-07' and '2015-02-10'
还有一张这样的表格:
0 = "id":1,
"pc_name":"BCL101",
"created_at":"2015-02-07 22:25:44"
2 = "id":2,
"pc_name":"BCL101",
"created_at":"2015-02-07 22:25:44"
3 = "id":3,
"pc_name":"BCL101",
"created_at":"2015-02-10 22:25:44"
当我查询我的对账单时,它只显示带有"2014-02-07"日期的前两行,而不包括带有"2015-02-10"的日期,当我的查询是BETWEEN dates"2014-2-07"answers"2015-02-10'时。
在查询中添加时间
SELECT * FROM bcl1_logs where created_at between '2015-02-07 00:00:00' and '2015-02-10 23:59:59'
value between X and Y
表示value >= x AND value < y
,因此省略了'2015-02-10'
。如果您想使用between
运算符并将其包含在内,则需要添加额外的一天:
SELECT *
FROM bcl1_logs
WHERE created_at BETWEEN '2015-02-07' AND '2015-02-11'
其他答案解释了将日期与日期和时间进行比较的问题,因此请尝试在查询中使用DATE()
:
SELECT * FROM bcl1_logs WHERE DATE(created_at) BETWEEN '2015-02-07' AND '2015-02-10'
如果只使用日期部分,则时间部分设置为00:00:00
。所以你的查询看起来像
created_at between '2015-02-07 00:00:00' and '2015-02-10 00:00:00'
并且这不包括具有时间22:25:44
的第三记录。
更改为:
created_at between '2015-02-07 00:00:00' and '2015-02-10 23:59:59'