Search code examples
coldfusioncfquery

How can I get particular Row in a query variable using ColdFusion?


Take the following query example:

<cfquery name="Test" Datasource = "TestDB">
    Select * from Table_Test
</cfquery>

Assume that the "Test" query returns 10 rows. I want to show single row on current time.

Note: I do not want to change the SQL statement.


Solution

  • If you want one random row from the query:

        <cfset start = randRange(1, Test.recordCount)>
        <cfoutput>
            #Test.name[start]#&nbsp;#Test.email[start]#<br>
        </cfoutput>
    

    No need to loop.

    NOTE: It is more efficient to modify the query to get a random row.

    How to request a random row in SQL?