Using Query Results in Queries

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.

Query of query benefits

Performing queries on query results has many benefits:

Creating queries of queries

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:

FROM 


WHERE 


GROUP BY 


UNION 


ORDER BY 


HAVING 


AS 


DISTINCT 

Boolean predicates:

LIKE 

NOT LIKE 

IN 

NOT IN 

BETWEEN 

NOT BETWEEN 

AND 

OR 

Aggregate functions:

Count([DISTINCT][*] expr) 

Sum([DISTINCT] expr) 

Avg([DISTINCT] expr) 

Max(expr) 

Min(expr) 

You cannot nest aggregate functions.
Comparison operators:

<= 

>= 

= 

< 

>

<> 


 

You can also do the following tasks:

Performing a query on a query

To generate a query using an existing query:


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.

To use the results of a query in a query:

  1. Create a new application page in ColdFusion Studio.
  2. Edit the page so that it appears as follows:
    <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>
    
    
  3. Save the page as queryquery.cfm in myapps under the Web root directory.
  4. Return to your browser and enter the following URL to view the results of the query:

    http://127.0.0.1/myapps/queryquery.cfm

  5. View the source in the browser.

Reviewing the code

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:
Code
Description

cfset ListNameSearch = "Jones" 

Set the last name to use in the second query. In a complete application, this information comes from user interaction.

<cfquery datasource = "CompanyInfo"

name = "EmpList"

cachedwithin=#CreateTimeSpan(0,1,0,0)#> 

  SELECT *  

  FROM Employee 

</cfquery> 

Query the database specified in the CompanyInfo data source and select all data in the Employee table. Cache the query data between requests to this page, and do not query the database if the cached data is less than an hour old.

<cfquery dbtype = "query" name = "QueryFromQuery" >  

  SELECT Emp_ID, FirstName,

    LastName 

  FROM Emplist 

  WHERE LastName='#LastNameSearch#' 

</cfquery> 

 

Use the EmpList query as the source of the data in a new query. This 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.

<cfoutput query = QueryFromQuery> 

  #Emp_ID#: #FirstName#  

  #LastName#<br> 

</cfoutput> 

<br> 

Use the QueryFromQuery query to display the list of employee IDs, first names, and last names.

<cfoutput> 

  #EmpList.columnlist#<br> 

</cfoutput> 

List all the columns returned by the Emplist query.

<cfoutput> 

  #QueryFromQuery.columnlist#<br> 

</cfoutput> 

List all the columns returned by the QueryFromQuery query.



Banner.Novgorod.Ru