在Mysql中保存视图

时间:2013-05-30 23:00:34

标签: mysql coldfusion views

我有一个Mysql查询,我从中打印输出。我想将此查询保存为视图,以便我可以从视图中打印聚合输出。在这个非常简单的示例中,我创建了视图,然后针对它运行两个查询。一切都很好。但是,似乎我已经去过数据库3次了 - 一次用于视图,两次用于查询。通过执行第一个查询,请求进入视图,然后执行第二个查询,可以只对数据库进行两次。我在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> 

0 个答案:

没有答案