After you have created a record set with a tag or function, you can retrieve data from the record set in one or more dependent queries. A query that retrieves data from a record set is called a Query of Queries. A typical use of a Query of Queries is to retrieve an entire table into memory with one query, and then access the table data (the record set) with subsequent sorting or filtering queries. In essence, you query the record set as if it were a database table.
Note: Because you can generate a record set in ways other than using the cfquery tag, the term In Memory Query is sometimes used instead of Query of Queries.
Benefits of Query of Queries
Performing a Query of Queries has many benefits, including the following:
- If you need to access the same tables multiple times, you greatly reduce access time, because the data is already in memory (in the record set).
A Query of Queries is ideal for tables of 5,000 to 50,000 rows, and is limited only by the memory of the ColdFusion host computer.
- You can perform joins and union operations on results from different data sources.
For example, you can perform a union operation on queries from different databases to eliminate duplicates for a mailing list.
- You can efficiently manipulate cached query results in different ways. You can query a database once, and then use the results to generate several different summary tables.
For example, if you need to summarize the total salary by department, by skill, and by job, you can make one query to the database and use its results in three separate queries to generate the summaries.
- You can obtain drill-down, master-detail information for which you do not access the database for the details.
For example, you can select information about departments and employees in a query, and cache the results. You can then display the employees' names. When users select an employee, the application displays the employee's details by selecting information from the cached query, without accessing the database.
- You can use a Query of Queries in report definitions to generate subreport data. For more information, see Using subreports.
Performing a Query of Queries
There are four steps to perform a Query of Queries.
- Generate a record set through a master query.
You can write a master query using a tag or function that creates a record set. For more information, see Creating a record set.
- Write a detail query--a cfquery tag that specifies dbtype="query".
- In the detail query, write a SQL statement that retrieves the relevant records. Specify the names of one or more existing queries as the table names in your SQL code. Do not specify a datasource attribute.
- If the database content does not change rapidly, use the cachedwithin attribute of the master query to cache the query results between page requests. This way, ColdFusion accesses the database on the first page request, and does not query the database again until the specified time expires. You must use the CreateTimeSpan function to specify the cachedwithin attribute value (in days, hours, minutes, seconds format).
The detail query generates a new query result set, identified by the value of the name attribute of the detail query. The following example illustrates the use of a master query and a single detail query that extracts information from the master.
Use the results of a query in a query
- Create a ColdFusion page with the following content:
<h1>Employee List</h1>
<!--- LastNameSearch (normally generated interactively) --->
<cfset LastNameSearch="Doe">
<!--- Master Query --->
<cfquery datasource="cfdocexamples" name="master"
cachedwithin=#CreateTimeSpan(0,1,0,0)#>
SELECT * from Employee
</cfquery>
<!--- Detail Query (dbtype=query, no data source) --->
<cfquery dbtype="query" name="detail">
SELECT Emp_ID, FirstName, LastName
FROM master
WHERE LastName=<cfqueryparam value="#LastNameSearch#"
cfsqltype="cf_sql_char" maxLength="20"></cfquery>
<!--- output the detail query results --->
<p>Output using a query of query:</p>
<cfoutput query=detail>
#Emp_ID#: #FirstName# #LastName#<br>
</cfoutput>
<p>Columns in the master query:</p>
<cfoutput>
#master.columnlist#<br>
</cfoutput>
<p>Columns in the detail query:</p>
<cfoutput>
#detail.columnlist#<br>
</cfoutput>
- Save the page as query_of_query.cfm in the myapps directory under the web_root.
- Display query_of_query.cfm in your browser
Reviewing the code
The master query retrieves the entire Employee table from the cfdocexamples data source. The detail query selects only the three columns to display for employees with the specified last name. The following table describes the code and its function:
Code
|
Description
|
cfset LastNameSearch="Doe"
|
Sets the last name to use in the detail query. In a complete application, this information comes from user interaction.
|
<cfquery datasource="cfdocexamples"
name="master"
cachedwithin=#CreateTimeSpan(0,1,0,0)#>
SELECT * from Employee
</cfquery>
|
Queries the cfdocexamples data source and selects all data in the Employees table. Caches the query data between requests to this page, and does not query the database if the cached data is less than an hour old.
|
<cfquery dbtype="query" name="detail">
SELECT Emp_ID, FirstName, LastName
FROM master
WHERE LastName=<cfqueryparam
value="#LastNameSearch#"
cfsqltype="cf_sql_char"
maxLength="20"></cfquery>
|
Uses the master query as the source of the data in a new query, named detail. This new query selects only entries that match the last name specified by the LastNameSearch variable. The query also selects only three columns of data: employee ID, first name, and last name. The query uses the cfqueryparam tag to prevent passing erroneous or harmful code.
|
<cfoutput query=detail>
#Emp_ID#: #FirstName# #LastName# <br>
</cfoutput>
|
Uses the detail query to display the list of employee IDs, first names, and last names.
|
<cfoutput>
#master.columnlist#<br>
</cfoutput>
|
Lists all the columns returned by the master query.
|
<cfoutput>
#detail.columnlist#<br>
</cfoutput>
|
Lists all the columns returned by the detail query.
|