ColdFusion allows you to use the results of a previous query in any cfquery
tag that returns row data to ColdFusion. You can query a database once and use the results in several dependent queries. Queries generated from other queries are often referred to as query of queries.
Performing queries on query results has many benefits:
For example, you can do a union on queries from different databases to eliminate duplicates for a mailing list.
For example, if you need to summarize the total salary by department, by skill, and job, you can make one query to the database and use its results in three separate queries to generate the summaries.
For example, you can select information about departments and employees in a query and cache the results. You can then display the employee names. When users select an employee, the application displays the employee details by selecting information from the cached query without accessing the database.
You can create a query using a query object from any ColdFusion tag or function that generates query results, including cfldap
, cfdirectory
, chttp
, cfstoredproc
, cfpop
, cfindex
, and the Query
functions.
You can use a limited subset of the SQL SELECT syntax, which includes:
You can also do the following tasks:
To generate a query using an existing query:
cfquery tag's
dbtype
attribute as "query".
datasource
attribute. cachedwithin
attribute to cache the query results of 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. Note that you must use the CreateTimeSpan
function to specify the cachedwithin
attribute value.
Note You cannot add a literal value as a column to the SELECT list in a query of queries. |
Your query generates a new query results set, identified by the value of the name
attribute. The following example illustrates the use of a master query and a single detail query that extracts information from the master. A more extended example would use multiple detail queries to get different information from the same master query.
<html> <head> <title>Using Query Results in a Query</title> </head> <body> <h1>Employee List</h1> <!--- LastNameSearch normally would be generated interactively ---> <cfset LastNameSearch = "Jones"> <!--- Normal query ---> <cfquery datasource = "CompanyInfo" name = "EmpList" cachedwithin=#CreateTimeSpan(0,1,0,0)#> SELECT * FROM Employee </cfquery> <!--- Query using query results ---> <cfquery dbtype = "query" name = "QueryFromQuery" > SELECT Emp_ID, FirstName, LastName FROM EmpList WHERE LastName = '#LastNameSearch#' </cfquery> Output using a query of query<br> <cfoutput query = QueryFromQuery> #Emp_ID#: #FirstName# #LastName#<br> </cfoutput> <br> Columns in the Emplist database query<br> <cfoutput> #Emplist.columnlist#<br> </cfoutput> <br> Columns in the QueryFromQuery query<br> <cfoutput> #QueryFromQuery.columnlist#<br> </cfoutput> </body> </html>
http://127.0.0.1/myapps/queryquery.cfm
The page retrieves the entire Employee table from the CompanyInfo database. A second query selects only the three columns to display for employees with the specified last name. The following table describes the code and its function: