ColdFusion - 查询的输出分组



我有一个很好的问题,可能很简单。 我有以下疑问:

<cfquery name="getempareview" dbtype="query">
SELECT firstname,lastname,deptname,supcode
FROM getreviews
WHERE supcode IN (#preserveSingleQuotes(setsupcode)#)
</cfquery>

我需要做的是输出,以便 supcode 在列表中包含其他数据。 因此,如果我有 100 行数据,并且其中 25 条记录上的 supcode 相同,只需有以下内容:

支持代码 名字姓氏 - 部门名称(所有 25 条记录将在此处列出(

任何帮助将不胜感激。

嵌套输出。试试这个。

<cfoutput query="YourQueryName" group="SupCode">
<h2>#SupCode#</h2>
<cfoutput>
#FirstName# #LastName# <br/>
</cfoutput>
</cfoutput>

您需要使用嵌套和分组输出。并向查询添加ORDER BY

<cfset setsupcode = "1,3,5">
<cfquery name="getempareview" dbtype="query">
SELECT firstname,lastname,deptname,supcode
FROM getreviews
WHERE supcode IN (<cfqueryparam value="#setsupcode#" cfsqltype="numeric" list="yes">)
ORDER BY supcode, deptname, lastname, firstname
</cfquery>
<cfoutput query="getempareview" group="supcode">
<h2>#supcode#</h2>
<cfoutput group="deptname">
#firstname# #lastname# (#deptname#) <br>
</cfoutput>
</cfoutput>

https://trycf.com/gist/763ede5485b0978504250f7f5baf9deb/acf11?theme=solarized_dark

此外,由于这显然是查询的查询,因此您可以在初始查询中更好地组织数据,而不必返回以重新处理数据。

最新更新