Paul Alkema

Discussions on Web Development and Security

Paul Alkema

Querying Query of Queries

April 9, 2010 · 1 Comment

ColdFusion have a build in function that allowed you to query structures. A query, in case you don't know is a structure. Basically this can be done by creating a query or structure, then by writing a query but instead of defining a datasource, you define a dbtype which should be set to "query". Then instead of using the name of the table you use the name of the structure that you're trying to query. The reason you would want to do this, is because if you pull a lot of data that relates to a loop normally you would do a query inside the loop, but using QoQ you can create one query above the loop, then while looping use QoQ to query the query that is above the loop. This will increase performance greatly.

EXAMPLE
<!--- THE QUERY --->
<cfquery name="Dude" datasource="#request.maindatasource#">
SELECT dude
FROM iod_dudes
WHERE dudeStatus = 'Awesome'
</cfquery>

<!--- THE QUERY OF QUERIES --->
<cfquery name="dudeQoQ" dbtype="query">
SELECT Dude
FROM Dude
WHERE dudeStatus2 = 'Really Awesome'
</cfquery>

<!--- QUERYING THE QUERY OF QUERIES --->
<cfquery name="dudeQtQoQ" dbtype="query">
SELECT Dude
FROM dudeQoQ
WHERE DudesName = 'Paul Alkema'
</cfquery>

Tags: ColdFusion