我正在尝试使用下面的cfml(sql)语句查询数据库,但我希望能够从数组中获取每个stat_instance的结果,最后一行包含循环,我认为应该生成一个查询数组中的每个值
select
<cfif monthCrossover>
statsMinEnd2 +
</cfif>
stats_min as time_stamp,
<cfif structKeyExists(avgThis, arguments.key)>
avg
<cfelse>
sum
</cfif>
(stats_value) as stats_value
from stats_kv_table_201207
where
<cfif dayStart == dayEnd>
stats_day = <cfqueryparam value="#dayStart#">
<cfelse>
stats_day >= <cfqueryparam value="#dayStart#">
and stats_day <= <cfqueryparam value="#dayEnd#">
</cfif>
and stats_min >= <cfqueryparam value="#statsMinStart1#"> and stats_min <= <cfqueryparam value="#statsMinEnd1#">
and stats_component = <cfqueryparam value="#arguments.component#">
<cfset instanceList = ListToArray(arguments.instance)>
<cfif ArrayLen(instanceList) EQ 1>
and stats_instance = <cfqueryparam value="#arguments.instance#">
</cfif>
<cfif ArrayLen(instanceList) GT 1>
and stats_instance IN (<cfqueryparam value="#arguments.instance#" list="yes"/>)
</cfif>
<cfif ArrayLen(instanceList) GT 1 and ArrayContains(instanceList, "multi")>
<cfloop index = "i" from="1" to="#arrayLen(instanceList)#" array =#instanceList#>
<cfset stats_instance = <cfqueryparam value= #instanceList[i]#>
</cfloop>
</cfif>
答案 0 :(得分:1)
如果我正确理解了您的问题,您可能需要在查询中使用GROUP BY子句按stats_instance对sum / avg值进行分组。像这样:
SELECT stats_instance, ...
FROM ...
WHERE
...
<cfif len(trim(arguments.instance))>
AND stats_instance IN (<cfqueryparam value="#arguments.instance#" list="yes"/>)
</cfif>
GROUP BY stats_instance
ORDER BY stats_instance
注意:语法可能因数据库引擎而异。
答案 1 :(得分:0)
这样做是否有问题?
<cfset myArr = ArrayNew(1) />
<cfloop array="myOtherArr" index="index">
<cfquery name="myQuery" datasource="myDataSource">...</cfquery>
<cfset ArrayAppend(myArr, myQuery.value) />
</cfloop>