我正在运行以下查询查询并收到错误:
时间戳:保存所有日期和时间相关值的列的名称。
MyDatabase:数据库名称
事件:名称为“Events”的另一列的名称,其中包含各种值,如FIRST,SECOND,THIRD等。我在这里提到了FIRST 方便和清晰。
<cfquery datasource = "XX.XX.X.XX" name="master">
SELECT count(Timestamp) as COUNT,date_format(Timestamp,'%m-%d-%Y')
FROM MyDatabase
WHERE EVENTS = "FIRST" GROUP BY Timestamp ;
</cfquery>
<cfquery dbtype="query" name="detail">
SELECT *
FROM master
WHERE Timestamp > <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_date"> AND Timestamp < <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_date">;
</cfquery>
就startdate和enddate参数而言,我已按如下方式设置它们:
<cfparam name="form.startdate" default="#dateformat(now()-5, 'mm/dd/yyyy')#">
<cfparam name="form.enddate" default="#dateformat(now()-1, 'mm/dd/yyyy')#">
<cfparam name="form.selectdate" default="#dateformat(now(), 'mm/dd/yyyy')#">
我收到了以下错误:
Error Executing Database Query.
Query Of Queries syntax error.
Encountered "Timestamp. Incorrect conditional expression, Expected one of [like|null|between|in|comparison] condition,
The error occurred in line 40
38 : SELECT *
39 : FROM master
40 : WHERE Timestamp > <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_date"> AND Timestamp < <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_date">;
41 :
42 :
有谁能告诉我出了什么问题?
更新
修复#
符号后,我现在收到以下错误:
Error Executing Database Query.
Query Of Queries syntax error.
Encountered "Timestamp. Incorrect conditional expression, Expected one of [like|null|between|in|comparison] condition,
The error occurred in line 40
38 : SELECT *
39 : FROM master
40 : WHERE Timestamp > <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_date"> AND Timestamp < <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_date">;
41 :
42 :
答案 0 :(得分:4)
value="#dateAdd('d', 1,form.enddate#)"
您的结束#
标志放错了地方。它应该是在后面的右括号:
value="#dateAdd('d', 1, form.enddate)#"
<强>更新强>
“时间戳”和“计数”是列名或别名的错误选择,因为它们是许多数据库中的保留字。正如阿德里安在评论中提到的,both are reserved words in CF QoQ's as well.。要在QoQ中使用“时间戳”,您必须将其封装在[]
中以使其脱离。从长远来看,您最好重命名列并完全避免问题。
WHERE [Timestamp] >= <cfqueryparam ....>
AND [Timestamp] < <cfqueryparam ....>
另外,SQL比较中存在轻微的错误。结束日期运算符应为<
。通过使用<=
,比较包括额外的分钟,因此它很可能会在午夜获取额外的记录,即dateAdd('d', 1, form.enddate)
。正确的比较是:
WHERE Timestamp >= <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_date">
AND Timestamp < <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_date">;
答案 1 :(得分:2)
您的代码中存在语法错误。
#dateAdd('d', 1,form.enddate#)
应该是:
#dateAdd('d', 1,form.enddate)#
答案 2 :(得分:0)
您是否尝试过使用BETWEEN关键字?
所以:
WHERE Timestamp > <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_date"> AND Timestamp < <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_date">;
会变成:
WHERE Timestamp BETWEEN <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_date" /> AND <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_date" />;
作为替代方案?