Mysql 介于仅显示第一行的运算符之间

Mysql between operator showing only the first row

我有这样的查询:

SELECT * FROM bcl1_logs where created_at between '2015-02-07' and '2015-02-10'

和这样的 table:

 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”的前两行,当我的查询是“2014 年之间”时,它不包括日期为“2015-02-10”的日期-02-07' 和 '2015-02-10'。

在您的查询中添加时间

SELECT * FROM bcl1_logs where created_at between '2015-02-07 00:00:00' and '2015-02-10 23:59:59'

如果只使用日期部分,时间部分设置为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'

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'