ColdFusion looping over query with cfqueryparam

322 views Asked by At

I am trying to loop over query with a nested query. The code that I have so far:

<cfloop query="hashTableLatest">
    <cfquery name="getDifferentImages" dbtype="query">
        SELECT image, imageHash
        FROM hashTable
        WHERE imageHash = <cfqueryparam cfsqltype="cf_sql_varchar" value="#hashTableLatest.imageHash#" />
    </cfquery>
</cfloop>

The problem that I have is that it doesn't loop dynamically through the cfqueryparam. It just gets the first value from the hashTableLatest. Can anyone tell me what am I doing wrong? How can I loop through a query and dynamically change the cfqueryparam?

EDITED To get all the information I need in a single query:

select a.imageHash
from tblHashLatest a
WHERE a.imageHash in (SELECT c.imageHash
                  FROM tblHash c
              WHERE a.imageHash <> c.imageHash)

I think that the above SQL should get me all the information I need. The result I am looking for is to get all imageHashes that are not same

1

There are 1 answers

2
Dan Bracuk On BEST ANSWER

There are a couple of options. One is to not use a loop and just do this:

WHERE imageHash in ( 
<cfqueryparam cfsqltype="cf_sql_varchar" 
value="#ValueList(hashTableLatest.imageHash)#" list="yes">
)
</cfquery

If possible, you should look for ways to get all the information you need from a single query.