ColdFusion 9.0 Resources |
Query of Queries user guideContents [Hide]If you know SQL or have interacted with databases, you might be familiar with some of the Query of Queries functionality. Using joinsA join operation uses a single SELECT statement to return a result set from multiple, related tables, typically those tables with a primary key - foreign key relationship. The two SQL clauses that perform joins are:
Note: Query of Queries supports joins between two
tables only.
Using unionsThe UNION operator lets you combine the results of two or more SELECT expressions into a single recordset. The original tables must have the same number of columns, and corresponding columns must be UNION-compatible data types. Columns are UNION-compatible data types if they meet one of the following conditions:
Note: Query Of Queries does not support ODBC-formatted
dates and times.
ExampleThis example uses the following tables:
To combine Table1 and Table2, use a UNION statement, as follows: SELECT * FROM Table1 UNION SELECT * FROM Table2 The UNION statement produces the following result (UNION) table:
Using aliases for column namesThe column names of a UNION table are the column names in the result set of the first SELECT statement in the UNION operation; Query of Queries ignores the column names in the other SELECT statement. To change the column names of the result table, you can use an alias, as follows: Select Type as SportType, Name as SportName from Table1 UNION Select * from Table2 Duplicate rows and multiple tablesBy default, the UNION operator removes duplicate rows from the result table. If you use the keyword ALL, then duplicates are included. You can combine an unlimited number of tables using the UNION operator, for example: Select * from Table1 UNION Select * from Table2 UNION Select * from Table3 ... Parentheses and evaluation orderBy default, the Query of Queries SQL engine evaluates a statement containing UNION operators from left to right. You can use parentheses to change the order of evaluation. For example, the following two statements are different: /* First statement. */ SELECT * FROM TableA UNION ALL (SELECT * FROM TableB UNION SELECT * FROM TableC ) /* Second statement. */ (SELECT * FROM TableA UNION ALL SELECT * FROM TableB ) UNION SELECT * FROM TableC In the first statement, there are no duplicates in the union between TableB and TableC. Then, in the union between that set and TableA, the ALL keyword includes the duplicates. In the second statement, duplicates are included in the union between TableA and TableB but are eliminated in the subsequent union with TableC. The ALL keyword has no effect on the final result of this expression. Using other keywords with UNIONWhen you perform a UNION, the individual SELECT statements cannot have their own ORDER BY or COMPUTE clauses. You can only have one ORDER BY or COMPUTE clause after the last SELECT statement; this clause is applied to the final, combined result set. You can only specify GROUP BY and HAVING expressions in the individual SELECT statements. Using conditional operatorsQuery of Queries lets you use the following conditional operators in your SQL statements: Comparison conditionalThis conditional lets you compare an expression against another expression of the same data type (Numeric, String, Date, or Boolean). You can use it to selectively retrieve only the relevant rows of a recordset. BETWEEN conditionalThis conditional lets you compare an expression against another expression. You can use it to selectively retrieve only the relevant rows of a recordset. Like the comparison conditional, the BETWEEN conditional also compares; however, the BETWEEN conditional compares against a range of values. Therefore, its syntax requires two values, which are inclusive, a minimum and a maximum. Separate these values with the AND keyword. IN conditionalThis conditional lets you specify a comma-delimited list of conditions to match. It is similar in function to the OR conditional. In addition to being more legible when working with long lists, the IN conditional can contain another SELECT statement. LIKE conditionalThis conditional lets you perform wildcard searches, in which you compare your data to search patterns. This strategy differs from other conditionals, such as BETWEEN or IN, because the LIKE conditional compares your data to a value that is partially unknown. Syntaxlike_cond ::= left_string_exp [NOT] LIKE right_string_exp [ESCAPE escape_char] The left_string_exp can be either a constant string, or a column reference to a string column. The right_string_exp can be either a column reference to a string column, or a search pattern. A search pattern is a search condition that consists of literal text and at least one wildcard character. A wildcard character is a special character that represents an unknown part of a search pattern, and is interpreted as follows:
Note: Earlier versions of ColdFusion do not support
bracketed ranges.
ExamplesThe following example uses the LIKE conditional to retrieve only those dogs of the breed Terrier, whether the dog is a Boston Terrier, Jack Russell Terrier, Scottish Terrier, and so on: SELECT dog_name, dog_IQ, breed FROM Dogs WHERE breed LIKE '%Terrier'; The following examples are select statements that use bracketed ranges: SELECT lname FROM Suspects WHERE lname LIKE 'A[^c]%'; SELECT lname FROM Suspects WHERE lname LIKE '[a-m]%'; SELECT lname FROM Suspects WHERE lname LIKE '%[]'; SELECT lname FROM Suspects WHERE lname LIKE 'A[%]%'; SELECT lname FROM Suspects WHERE lname LIKE 'A[^c-f]%'; Case sensitivityUnlike the rest of ColdFusion, Query of Queries is case-sensitive. However, Query of Queries supports two string functions, UPPER() and LOWER(), which you can use to achieve case-insensitive matching. ExamplesThe following example matches only 'Sylvester': SELECT dog_name FROM Dogs WHERE dog_name LIKE 'Sylvester'; The following example is not case sensitive; it uses the LOWER() function to treat 'Sylvester', 'sylvester', 'SYLVESTER', and so on, as all lowercase, and matches them with the all lowercase string, ‘sylvester’: SELECT dog_name FROM Dogs WHERE LOWER(dog_name) LIKE 'sylvester'; If you use a variable on the right side of the LIKE conditional and want to ensure that the comparison is not case-sensitive, use the LCase or UCase function to force the variable text to be all of one case, as in the following example: WHERE LOWER(dog_name) LIKE '#LCase(FORM.SearchString)#'; Managing data types for columnsA Query of Queries requires that every column has metadata that defines the data type of the column. All queries that ColdFusion creates have metadata. However, a query created with QueryNew function that omits the second parameter does not contain metadata. You use this optional second parameter to define the data type of each column in the query. Specify column data types in the QueryNew function Type
a QueryNew function, specifying the column names
in the first parameter and the data types in the second parameter,
as the following example shows:
<cfset qInstruments = queryNew("name, instrument, years_playing", "CF_SQL_VARCHAR, CF_SQL_VARCHAR, CF_SQL_INTEGER")> Note: To see the metadata for a Query of Queries, use
the GetMetaData function.
Specify the column data types in the QueryAddColumn function
If you do not specify the data type, ColdFusion examines the first 50 rows of each column to determine the data type when performing conditional expressions. In some cases, ColdFusion can guess a data type that is inappropriate for your application. In particular, if you use columns in a WHERE clause or other conditional expression, the data types must be compatible. If they are not compatible, use the CAST function to recast one of the columns to a compatible data type. For more information on casting, see Using the CAST function. For more information on data type compatibility, see Understanding Query of Queries processing. Note: Specifying the data type in the QueryNew function
helps you avoid compatibility issues.
Using the CAST functionIn some cases, the data type of a column is not compatible with the processing you want to do. For example, query columns returned by the cfhttp tag are all of type CF_SQL_VARCHAR, even if the contents are numeric. In this case, use the Query of Queries CAST function to convert a column value into an expression of the correct data type. The syntax for the CAST function is as follows: CAST ( expression AS castType ) Where castType is one of the following:
For example: <cfhttp url="http://quote.yahoo.com/download/quotes.csv?Symbols=csco,jnpr&format=sc1l1&ext=.csv" method="GET" name="qStockItems" columns="Symbol,Change,LastTradedPrice" textqualifier="""" delimiter="," firstrowasheaders="no"> <cfoutput> <cfdump var="#qStockItems#"> <cfdump var="#qStockItems.getColumnNames()#"> </cfoutput> <cfoutput> <cfloop index="i" from="1" to="#arrayLen(qStockItems.getColumnNames())#"> #qStockItems.getMetaData().getColumnTypeName(javaCast("int",i))#<br/> </cfloop> </cfoutput> <cftry> <cfquery name="hello" dbtype="query"> SELECT SUM(CAST(qStockItems.LastTradedPrice as INTEGER)) AS SUMNOW from qStockItems </cfquery> <cfcatch>Error in Query of Queries</cfcatch> </cftry> <cfoutput> <cfdump var="#hello#"> </cfoutput> Using aggregate functionsAggregate functions operate on a set of data and return a single value. Use these functions for retrieving summary information from a table, as opposed to retrieving an entire table and then operating on the recordset of the entire table. Consider using aggregate functions to perform the following operations:
Since not every relational database management system (RDBMS) supports all aggregate functions, refer to the documentation of your database. The following table lists the aggregate functions that Query of Queries supports:
Using group by and having expressionsQuery of Queries supports the use of any arbitrary arithmetic expression, as long as it is referenced by an alias. ExamplesThe following code is correct: SELECT (lorange + hirange)/2 AS midrange, COUNT(*) FROM roysched GROUP BY midrange; The following code is correct: SELECT (lorange+hirange)/2 AS x, COUNT(*) FROM roysched GROUP BY x HAVING x > 10000; The following code is not supported in Query of Queries: SELECT (lorange + hirange)/2 AS midrange, COUNT(*) FROM roysched GROUP BY (lorange + hirange)/2; Using ORDER BY clausesQuery of Queries supports the ORDER BY clause to sort. Make sure that it is the last clause in your SELECT statement. You can sort by multiple columns, by relative column position, by nonselected columns. You can specify a descending sort direction with the DESC keyword (by default, most RDBMS sorts are ascending, which makes the ASC keyword unnecessary). ExampleThe following example shows a simple sort using an ORDER BY clause: SELECT acetylcholine_levels, dopamine_levels FROM results ORDER BY dopamine_levels The following example shows a more complex sort; results are first sorted by ascending levels of dopamine, then by descending levels of acetylcholine. The ASC keyword is unnecessary, and is used only for legibility. SELECT acetylcholine_levels, dopamine_levels FROM results ORDER BY 2 ASC, 1 DESC Using aliasesQuery of Queries supports the use of database column aliases. An alias is an alternate name for a database field or value. Query of Queries lets you reuse an alias in the same SQL statement. One way to create an alias is to concatenate (append) two or more columns to generate a value. For example, you can concatenate a first name and a last name to create the value fullname. Because the new value does not exist in a database, you refer to it by its alias. The AS keyword assigns the alias in the SELECT statement. ExamplesQuery of Queries supports alias substitutions in the ORDER BY, GROUP BY, and HAVING clauses. Note: Query of Queries does not support aliases for
table names.
SELECT FirstName + ' ' + LastName AS fullname from Employee; The following examples rely on these two master queries: <cfquery name="employee" datasource="2pubs"> SELECT * FROM employee </cfquery> <cfquery name="roysched" datasource="2pubs"> SELECT * FROM roysched </cfquery> ORDER BY example<cfquery name="order_by" dbtype="query"> SELECT (job_id || job_lvl)/2 AS job_value FROM employee ORDER BY job_value </cfquery> Handling null valuesQuery of Queries uses Boolean logic to handle conditional expressions. Proper handling of NULL values requires the use of ternary logic. The IS [NOT] NULL clause works correctly in Query of Queries. However the following expressions do not work properly when the column breed is NULL: WHERE (breed > 'A') WHERE NOT (breed > 'A') The correct behavior should not include NULL breed columns in the result set of either expression. To avoid this limitation, add an explicit rule to the conditionals and rewrite them in the following forms: WHERE breed IS NOT NULL AND (breed > 'A') WHERE breed IS NOT NULL AND not (breed > 'A') Concatenating stringsQuery of Queries support two string concatenation operators: + and ||, as the following examples show: LASTNAME + ', ' + FIRSTNAME LASTNAME || ', ' || FIRSTNAME Escaping reserved keywordsColdFusion has a list of reserved keywords, which are typically part of the SQL language and are not normally used for names of columns or tables. To escape a reserved keyword for a column name or table name, enclose it in brackets. Important: Earlier versions of ColdFusion let you
use some reserved keywords without escaping them.
ExamplesQuery of Queries supports the following SELECT statement examples: SELECT [from] FROM parts; SELECT [group].firstname FROM [group]; SELECT [group].[from] FROM [group]; Query of Queries does not support nested escapes, such as in the following example: SELECT [[from]] FROM T; The following table lists ColdFusion reserved keywords:
Using Queries of Queries with datesIf you create a query object with the QueryNew function and populate a column with date constants, ColdFusion stores the dates as a string inside the query object until a Query of Queries is applied to the query object. When ColdFusion applies a Query of Queries to the query object, it converts the string representations into date objects. Query of Queries supports date constants in SQL and ODBC format, as follows:
If you want to convert the date to its original
format, use the DateFormat function and apply the "mm/dd/yy" mask. Understanding Query of Queries performanceQuery of Queries performs well on single-table query objects that were accessed directly from a database. This happens because ColdFusion stores meta information for a query object accessed from a database. When working with a query resulting in a SQL join, Query of Queries performs as follows:
Understanding Query of Queries processingQuery of Queries can process the following:
Comparing columns with different data typesStarting with ColdFusion MX 7, ColdFusion includes enhancements that allow you to compare columns with different data types. If one of the operands has a known column type (only constants have an unknown column type), Query of Queries tries to coerce the constant with an unknown type to the type of the operand with metadata. The pairs of allowed coercions are as follows:
That is, ColdFusion can coerce between binary and string, but not between date and string. If both operands have known data types, the types must be the same. The only exception is that ColdFusion can coerce among integer, float, and double. If both operands are constants, ColdFusion tries to coerce the values, first to the most restrictive type, then to the least restrictive type.
Passing queries by referenceA Query of Queries is copied by reference from its related query; which means that ColdFusion does not create a query when you create a Query of Queries. It also means that changes to a Query of Queries, such as ordering, modifying, and deleting data, are also applied to the base query object. If you do not want the original query to change, use the Duplicate function to create a copy and create the Query of Queries using the copied query. |