【发布时间】:2020-11-19 03:13:47
【问题描述】:
我在 Laravel Eloquent 中有这个查询:
$measures = Measure::groupBy('time')
->selectRaw('time, sum("delta") as sum_delta, sum("redistributed") as sum_redistr')
->where('operation_id', 'ACC0000001')
->where('time', '>', '2020-05-09 00:00')
->where('time', '<', '2020-05-10 00:00')
->where('source', 'source1')
->where('conso_prod', 'Conso')
->get()
当我使用toSql() 函数进行调试,然后将其粘贴到 pgAdmin 中时,我得到了正确的结果。
select time, sum("delta") as sum_delta, sum("redistributed") as sum_redistr from "measures"
where "operation_id" = 'ACC0000001'
and "time" > '2020-05-09' and "time" < '2020-05-10' and "source" = 'source1' and "conso_prod" = 'Conso' group by "time"
我每 30m 有一个结果,这是正确的。
但是当我使用 eloquent 时,我的行数相同,但所有 time 字段都相同:
"2020-05-09 00:00"
而不是递增。
我不明白为什么?我使用带有 TimescaleDB 扩展的 PostgreSQL。
【问题讨论】:
-
快速提问;
time是measures表上的列还是 PGSql 关键字?有时,当您使用selectRaw()时,您需要在列周围使用波浪号(如时间)以避免这种情况。话虽如此,那是针对 MySQL 的;我对 PGSql 的工作不多,所以我不能说这是否是个问题。 -
“时间”列是保存时间值还是日期时间值?
-
time 是具有时间戳值的列。它也是一个特殊的列,因为 TimescaleDB 是一个时间序列数据库。
标签: laravel postgresql eloquent timescaledb