You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GridDB查询指定时间范围时序数据时出现NOW()语法错误

GridDB时间序列查询:NOW()语法错误的解决方法

问题原因

GridDB使用的是TQL(时间序列查询语言),和标准SQL语法不完全兼容:

  • 不支持标准SQL的NOW()函数,替代的内置函数是CURRENT_TIMESTAMP
  • 不支持INTERVAL '1 DAY'这种时间间隔写法,时间运算需要直接使用毫秒数(1天=86400000毫秒)

你的原查询用了标准SQL的语法,所以触发「Syntax Error near 'NOW'」错误。

正确的查询方案

方案1:Python端计算时间范围(推荐)

在Python中提前算出24小时前的时间,再通过参数化查询传入,避免TQL语法限制,同时提高查询灵活性:

from griddb_python import *
from datetime import datetime, timedelta

factory = StoreFactory.get_instance()
gridstore = factory.get_store(host="127.0.0.1", port=10001, cluster_name="myCluster", username="admin", password="admin")

# Access the container
container = gridstore.get_container("sensor_data")

# 计算24小时前的时间
one_day_ago = datetime.now() - timedelta(days=1)
# 转换为GridDB支持的TIMESTAMP格式(ISO 8601字符串或datetime对象)
query = container.query("SELECT * WHERE timestamp > ?", one_day_ago)
rs = query.fetch()

while rs.has_next():
    data = rs.next()
    print(data)

方案2:使用GridDB内置函数和毫秒运算

如果要在TQL中直接计算时间范围,需要用CURRENT_TIMESTAMP获取当前时间,再减去对应毫秒数:

from griddb_python import *

factory = StoreFactory.get_instance()
gridstore = factory.get_store(host="127.0.0.1", port=10001, cluster_name="myCluster", username="admin", password="admin")

# Access the container
container = gridstore.get_container("sensor_data")

# 1天=86400秒=86400000毫秒
query = container.query("SELECT * WHERE timestamp > CURRENT_TIMESTAMP - 86400000")
rs = query.fetch()

while rs.has_next():
    data = rs.next()
    print(data)

语法确认

你的原查询存在两个语法问题:

  1. 用了GridDB不支持的NOW(),应替换为CURRENT_TIMESTAMP
  2. 用了不支持的INTERVAL '1 DAY',需替换为毫秒数运算,或在客户端计算时间后传入参数

内容的提问来源于stack exchange,提问作者Kenny Anaga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 00:29:50