0

我有一个 Mysql 查询,我从中打印输出。我想将此查询保存为视图,以便可以从视图中打印聚合输出。在这个非常简化的示例中,我创建了视图,然后针对它运行两个查询。一切正常。但是,似乎我已经访问了数据库 3 次——一次用于视图,两次用于查询。通过执行第一个查询 REQUESTING THAT BE TURNED INTO A VIEW,然后执行第二个查询,可以只访问数据库两次。我在 Cold Fusion 中并尝试过查询查询,但即使是下面的简单示例也太有限了。

<cfquery name = "viewtest" datasource = "Moxart">

alter view testview AS
select PersonFn, PersonLn, PersonState, dec1 from Person
where PersonState = <cfqueryparam value = "PA">
OR PersonState =  <cfqueryparam value = "NJ">
order by PersonState,PersonLN
</cfquery>

<cfquery name = "test" datasource = "Moxart">
select * from testview
</cfquery>

<table>
<cfoutput>
<cfloop array = #test.getColumnList()# index = "col">
<td>#col#</td>
</cfloop>
</cfoutput>

<cfoutput query = "test">
<tr>
<cfloop array = #test.getColumnList()# index = "col">
<td>#test[col][currentrow]#</td>
</cfloop>
</tr>
</cfoutput>

</table>

<cfset j = 0>
<cfloop array = #test.getColumnList()# index = "scol">  
<cfset j = j + 1>
   <cfif j EQ 1>
   <cfset aggcnt = "count(#scol#)"> 
   </cfif>   
<cfset aggarr[j] ="sum(#scol#)">             
</cfloop>

<cfset aggls = ArrayToList(aggarr)>

<cfoutput>
<cfquery name = "vtest" datasource = "Moxart">
select #aggls#, #aggcnt# from testview
group by PersonState
</cfquery>
</cfoutput>

<table>
<tr>
<cfoutput>
<cfloop array = #vtest.getColumnList()# index = "col">
<td>#col#</td>
</cfloop>
</cfoutput>

<cfoutput query = "vtest">
<tr>
<cfloop array = #vtest.getColumnList()# index = "col">
<td>#vtest[col][currentrow]#</td>
</cfloop>
</tr>

</cfoutput>
</table> 
4

0 回答 0