4

考虑以下:

<cfoutput query="resources" group="type">
    <h4>#type#</h4>
       <cfoutput>
          #name#
       </cfoutput>
 </cfoutput>

resources.recordcount会给我记录的总数,但是有没有一种优雅的方法可以找出嵌套数据的记录数?例如

<cfoutput query="resources" group="type">
    <h4>#type# (#noofrecords# of #resources.recordcount#)</h4>
       <cfoutput>
          #name#
       </cfoutput>
 </cfoutput>

我可能可以用循环做一些 hacky,但想知道是否有一种方法可以专门使用 cfoutput 组。

4

5 回答 5

8

您可以先进行输出以获取计数。这将比查询查询更有效。

<cfoutput query="resources" group="type">
  <cfset noofrecords= 0>
  <cfoutput>
    <cfset noofrecords++>
  </cfoutput>
  <h4>#type# (#noofrecords# of #resources.recordcount#)</h4>
  <cfoutput>
    #name#
  </cfoutput>
</cfoutput>
于 2014-11-07T13:16:23.920 回答
6

恐怕你必须自己数数。嵌套分组输出没有 RecordCount,因为它实际上是所有相同的查询,CF 只是为您做一些格式化。

于 2014-11-07T10:03:16.930 回答
2

如果您想要一种替代方法,您可以在原始查询中获取计数,但是,您需要在沿着这条路线走之前测试性能(尽管使用查询的查询也可能存在性能问题,因为没有索引)。

SELECT f1.id, f1.name, f1.type, f2.typeCount
FROM foo f1
INNER JOIN (
    SELECT COUNT(type) as typeCount, type
    FROM foo
    GROUP BY type 
) f2 on f1.type = f2.type 
ORDER BY f1.type, f1.name

然后在 CF 中你可以这样做:

<cfoutput query="resources" group="type">
    <h4>#resources.type# (#resources.typeCount# of #resources.recordCount#)</h4>
    <cfoutput>
      #resources.name#
    </cfoutput>
</cfoutput>

附带说明一下,在 CF10 中,您还可以在 cfloop 中使用如下查询进行分组:

<cfloop query="resources" group="type">
    <h4>#resources.type# (#resources.typeCount# of #resources.recordCount#)</h4>
    <cfloop>
      #resources.name#
    </cfloop>
</cfloop>
于 2014-11-07T10:41:54.760 回答
2

另一种解决方案是:

<!--- I chose the pipe delimiter | names of things often contain commas,
      otherwise, use any delimiter you like --->
<cfset TypesList = Valuelist(resources.type,"|")>

<cfoutput query="resources">
  <h4>#Type# (#ListValueCount(TypesList,Type,"|")# of #resources.recordcount#)</h4>
  <cfoutput>#name#</cfoutput>
</cfoutput>

虽然这也适用于其他应用程序,但它并不适用于所有应用程序。它期望类别计数(此处为“类型”)是唯一的(没有两个具有相同名称的类型)。处理此问题的更好方法是基于 ID 而不是名称进行计数。例如,您可能有这样的类别设置

Fruits
  Red Ones
    Strawberries
    Raspberries
  Yellow Ones
    Bananas
Vegetables
 Green ones
   Green peppers
 Red Ones
   Red Peppers
   Tomatoes

基于类型(作为字符串),输出会说 Red Ones (4)、Yellow Ones (1)、Green Ones (1)、Red Ones (4),但是在类别-产品关系表结构中,计数基于类别的唯一 ID 将检索准确的计数。

于 2014-11-07T16:09:57.193 回答
-1

要获取嵌套数据的计数并将其显示在您想要的位置,我会这样做:

<cfoutput query = "rescources" group = "type">
query of queries to get the count this type
output the type and the count
<cfoutput>
output nested data
</cfoutput>
</cfoutput>
于 2014-11-07T10:31:34.503 回答