22

我正在尝试将 ColdFusion 查询列转换为列表,最好的方法是什么?

我认为有一个内置函数可以让人们轻松地将查询的列转换为列表,如果有的话?

4

3 回答 3

51

有一个内置函数可以做到这一点:ValueList

<cfset myList = ValueList(query.columnname)>

与所有列表函数一样,有一个可选的分隔符属性。

<cfset myList = ValueList(query.columnname,"|")>

如果您需要列表中的值用双引号括起来,请使用 QuotedValueList。

<cfset myList = QuotedValueList(query.columnname)>
于 2011-11-23T18:39:32.443 回答
4

如果这适用于您要执行的操作,您还可以直接将查询的列作为数组访问,而无需进行任何转换:

qry.col[1] // col field of first record
qry.col[2] // col field of second record
...

或者

qry["col"][1] // col field of first record
qry["col"][2] // col field of second record

CF 查询对象实际上是一个关联的列数组……很奇怪但偶尔有用。

于 2011-03-30T17:49:05.293 回答
1

在这样的情况下怎么样:

<cfset SummaryQuery = Evaluate('getReportData' & summaryName & 'Summary') />
<cfset TypeList = ArrayToList(SummaryQuery[subsectionName & 'Type']) />

对比

<cfset QueryColumn = SummaryQuery[subsectionName & 'Type'] />
<cfset TypeList = ValueList(QueryColumn) />
于 2012-04-27T14:13:39.727 回答