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

2

5

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.

Counterblow answered 27/10, 2014 at 11:49 Comment(2)
The sentence, "I want to show single row on current time.", is not clear.Zacek
I second what @DanBracuk says. I think CFML_Developer has answered your question, but that's predicated on guessing what you mean in that highlighted sentence.Gainly
C
3

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?

Crude answered 27/10, 2014 at 21:5 Comment(2)
I'm not sure how you deduced that he wanted a random row, but kudos on taking that away from this question!Akbar
He had answered his question and it looked like that was what he was trying to do. That answer has been removed so it now looks like I can read minds. ;)Crude
D
7

If you know your row number, Test.columnName[RowNumber] will show you the value of the columnName in specified row number.

Danicadanice answered 27/10, 2014 at 12:18 Comment(1)
Yes brother you are right.But the table has minimum 50 column any way Thank you for answering this question.Counterblow
C
3

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?

Crude answered 27/10, 2014 at 21:5 Comment(2)
I'm not sure how you deduced that he wanted a random row, but kudos on taking that away from this question!Akbar
He had answered his question and it looked like that was what he was trying to do. That answer has been removed so it now looks like I can read minds. ;)Crude

© 2022 - 2024 — McMap. All rights reserved.