1

我试图通过一次查询多个类别的大量项目来保存我的数据库服务器的一些请求,然后使用<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 上,所以我们需要单独的表。

我遇到的问题是,如果没有返回 category="A" 的记录,我希望“A 事物表”说“无结果”,但 RecordCount 似乎适用于整个查询。有什么办法可以说类似的<cfif #queryList.RecordCount# WHERE #category# eq "A" GT "0">吗?

4

3 回答 3

3

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>
于 2018-06-25T15:53:29.077 回答
1

我相信您正在尝试执行以下操作。<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>
于 2018-06-25T15:24:42.640 回答
0

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>
于 2018-06-26T13:43:56.353 回答