调用数据库返回不完整的数据

Call to DB returning incomplete data

我有一个 SQL 查询:

select count(*) AS revolutions, DATE_FORMAT(time, '%Y-%m-%d %H') as time_period from `raw_data` where `time` >= '2016-09-10 21:51:33' group by `time_period`

此returns MySQL 客户端中的以下数据:

revolutions | time_period
630         | 2016-09-10 23
2062        | 2016-09-11 00
1839        | 2016-09-11 01
377         | 2016-09-11 02
83          | 2016-09-11 03
325         | 2016-09-11 04

在 Laravel 中,我构建了一个相同的查询,其转储如下所示:

["sql"]=>
  string(136) "select count(*) AS revolutions, DATE_FORMAT(time, '%Y-%m-%d %H') as time_period from `raw_data` where `time` >= ? group by `time_period`"
  ["bindings"]=>
  array(1) {
    [0]=>
    string(19) "2016-09-10 22:02:02"
  }

但是这returns下面的一组数据:

[
  {

    "revolutions": 1863,
    "time_period": "2016-09-10 22"
  },
  {
    "revolutions": 1839,
    "time_period": "2016-09-10 23"
  },
  {
    "revolutions": 377,
    "time_period": "2016-09-11 00"
  },
  {
    "revolutions": 83,
    "time_period": "2016-09-11 01"
  },
  {
    "revolutions": 325,
    "time_period": "2016-09-11 02"
  }
]

什么可能导致 02 上的数据丢失以及 03 和 04 上不存在的数据?

编辑:Lumen 中未设置时区。

修复是将配置值添加到 .env

DB_CONNECTION=mysql
DB_TIMEZONE=+02:00

看来 time 属于 timestamp 类型,它将时间转换为客户端应在连接后立即明确指定的时区。

要明确设置它,必须发出

SET time_zone = timezone;

查询。

并且默认值来自配置(或 CLI 参数)指令 default-time-zone

参考文献: