Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 循环或输出中的最后和第一个内容(coldfusion)_Database_Loops_Coldfusion - Fatal编程技术网

Database 循环或输出中的最后和第一个内容(coldfusion)

Database 循环或输出中的最后和第一个内容(coldfusion),database,loops,coldfusion,Database,Loops,Coldfusion,我想知道是否有可能在ColdFusion中显示从循环或输出中获取的特定内容,例如,我有一个输出: <cfoutput query="get_service_plus"><b>#SUBJECT#</b><br/>#plus_content#<br/></cfoutput> 以及它的查询,以防万一: <cfquery name="GET_SERVICE_PLUS" datasource="#DSN3#">

我想知道是否有可能在ColdFusion中显示从循环或输出中获取的特定内容,例如,我有一个输出:

<cfoutput query="get_service_plus"><b>#SUBJECT#</b><br/>#plus_content#<br/></cfoutput>
以及它的查询,以防万一:

<cfquery name="GET_SERVICE_PLUS" datasource="#DSN3#">
    SELECT 
        *
    FROM 
        SERVICE_PLUS
    WHERE   
        SERVICE_ID = #attributes.action_id#
      <cfif isDefined("GET_SERVICE_PLUS.SERVICE_PLUS_ID")>
        AND SERVICE_PLUS_ID = #GET_SERVICE_PLUS.SERVICE_PLUS_ID#
      </cfif>
      ORDER BY PLUS_DATE DESC,RECORD_DATE DESC
</cfquery>

我知道我很可能应该使用循环从db获取特定内容,但不知道如何实现它。。。谢谢你的帮助

我认为这应该从CF的角度来看:

<cfoutput query="get_service_plus">

  <cfif get_service_plus.currentRow eq 1 or get_service_plus.currentRow eq get_service_plus.recordCount>
    <b>#SUBJECT#</b><br/>#plus_content#<br/>
  </cfif>

</cfoutput>
您还可以在返回初始记录集后使用查询查询来执行此操作

我建议您尝试像您建议的那样抓取数据库层的第一行和最后一行,因为这样效率更高,特别是对于大型记录集


希望有帮助

你可以做几件事

如果要循环整个查询,可以使用变量qet_service_plus.currentrow检查当前行数,因此

<cfif qet_service_plus.currentrow eq 1>
   <!--- do first row display stuff --->
</cfif>
每个查询还包含查询中返回的记录数。你可以在recordcount中找到它,所以

<cfif get_service_plus.currentrow eq get_service_plus.recordcount>
   <!--- do last row display stuff --->
</cfif>
如果希望在查询中获取特定记录而不进行完整查询,可以将cfquery视为关联数组。例如

<cfoutput>
<!--- service id in first record --->
#get_service_plus['service_id'][1]# 
<!--- service id in last record --->
#get_service_plus['service_id'][get_service_plus.recordcount]# 
</cfoutput>

确保使用避免SQL注入。还有,您使用的是什么数据库服务器?在简单的查询循环中不需要增加值。每个查询都有currentrow和recordcount。参见文档了解cfquery用法:+1非常全面。我仍然会在数据库级别这样做,以避免读取非常大的记录集。如果您知道根据上面的查询是按日期字段排序的这一事实需要最新和最旧的记录,那么where子句根据max/min date返回记录就相对简单了。