条件 CF 记录计数

Conditional CF Record Count

我试图通过一次查询来自多个类别的大量项目,然后使用 <cfif ... > 语句将这些结果过滤到我正在显示的唯一表格中,从而为我的数据库服务器节省一些请求对于每个类别。我正在寻找返回的每个类别的记录数,而不仅仅是整个查询的记录数。

基本代码为:

<cfinvoke component="..." method="..." returnvariable="session.queryList">
    ...
</cfinvoke>

<cfoutput #session.queryList#>
    <cfif #category# eq "A">
        [Table for A things]
    </cfif>
    <cfif #category# eq "B">
        [Table for B things]
    </cfif>
    <cfif #category# eq "C">
        [Table for C things]
    </cfif>
</cfoutput>

我不想在这里使用 "ORDER BY category" 因为表格实际上位于我们隐藏和显示的不同 div 上,所以我们需要单独的表格。

我 运行 遇到的问题是,如果类别 ="A" 没有返回记录,我希望 "Table for A Things" 说 "No results",但是 RecordCount似乎适用于整个查询。有什么办法可以按照 <cfif #queryList.RecordCount# WHERE #category# eq "A" GT "0">?

的方式说些什么吗?

我相信您正在尝试执行以下操作。 <cfoutput> 有一项功能可以帮助您对查询结果进行分组,因为查询是按分组项目排序的。

<cfoutput query="#session.queryList#" group="category">
  <h3>Category #category#</h3>
  <table>
    <tr><th>...</th><th>...</th></tr>
    <cfoutput><!--- This cfoutput loops through the each record in the group. --->
      ---rows---
    </cfoutput>
  <table>
</cfoutput>

QoQ 可以提供帮助。

<cfinvoke component="..." method="..." returnvariable="session.queryList">
 ...
</cfinvoke>
 <!---then run QoQ on it--->
<cfquery name="catA" dbtype="query">
 select * from session.queryList where category ="A"
</query>
<cfquery name="catB" dbtype="query">
 select * from session.queryList where category ="B"
</query>
<cfif catA.recordcount>
 <cfoutput query="catA">
  [Table for A things]
 </cfoutput>
 <cfelse>
  No Records for A things
</cfif>
<cfif catB.recordcount>
 <cfoutput query="catB">
  [Table for B things]
 </cfoutput>
 <cfelse>
  No Records for B things
</cfif>

QofQ 很慢。您可以通过 mySQL:

的单次往返来完成此操作
SELECT someColumn, category, count(*) AS categoryCount 
FROM theTable
GROUP BY category
ORDER BY category, someColumn

分组将为您提供可在 CFML 中使用的每个类别的计数。

<cfoutput query="session.queryList" group="category">
    <cfif categoryCount eq 0>
        No Records for #category#. =(
    </cfif>
    <cfoutput>
        #someColumn#<br>
    </cfoutput>
</cfoutput>