2016-11-16 249 views
0
SELECT  
    to_char(messages. TIME, 'YYYY/MM/DD') AS FullDate, 
    to_char(messages. TIME, 'MM/DD') AS PartialDate, 
      COUNT(CASE WHEN message_definitions.error_category = ? THEN 1 END) AS Errors, 
    error_categories.threshold, 
    COUNT(CASE WHEN messages.message_id = 14 THEN 1 END) AS Picks 
FROM  
    messages LEFT JOIN 
    message_definitions USING (message_id) LEFT JOIN 
    error_categories USING (error_category) 
WHERE 
    (messages. TIME > TIMESTAMP ? - '30 day'::INTERVAL) AND 
    (messages. TIME < TIMESTAMP '2016-08-03' + '1 day'::INTERVAL) AND 
    (messages.system_id = ?) AND 
    (messages.message_id = 14 OR 
    message_definitions.error_category = ?) 
GROUP BY 
    FullDate, PartialDate, error_categories.threshold 
ORDER BY 
    FullDate DESC LIMIT 40 

在上面的查询,在where子句,(第一行:。?(消息TIME>时间戳 - '30天'::间隔) )当参数类型强制转换(即当我把之前TIMESTAMP?)我收到以下错误

enter image description here

这是不是在第二行文本数据真实(消息。TIME < TIMESTAMP“2016年8月3日” +“ 1天':: INTERVAL)和

当我将该子句更改为(消息。 TIME>? :: TIMESTAMP - '30 day':: INTERVAL)和 ) (即我把TIMESTAMP放在?之后?) 任何人都可以对发生的事情有所了解吗?谢谢!!

注: PostGresVersion - 9.6 ODBC驱动程序 - 32位驱动程序(位于C:\ Program Files文件(x86)的\ psqlODBC \ 0905 \ BIN \ psqlodbc30a.dll) 同样是Postgres的8.4也是如此。

+0

将参数绑定到上述查询的代码在哪里?你是否试图逐字运行上述查询? –

+0

是目前我正在逐字运行查询。 – user6868820

回答

0

The documentation说:

A constant of an arbitrary type can be entered using any one of the following notations:

type 'string'
'string'::type
CAST ('string' AS type)

The string constant's text is passed to the input conversion routine for the type called type. The result is a constant of the indicated type.

[...]

The :: , CAST() , and function-call syntaxes can also be used to specify run-time type conversions of arbitrary expressions, as discussed in Section 4.2.9. To avoid syntactic ambiguity, the type 'string' syntax can only be used to specify the type of a simple literal constant.

所以,你可以为你正在尝试做使用此语法仅用于字符串,而不是用一个参数。