40
Developing Web Applications with ColdFusion
Creating Table Joins
Many times, the data that you want to retrieve is maintained in multiple tables. For
example, in the database that you’re working with:
•
Department information is maintained in the Departments table. This includes
department ID numbers.
•
Employee information is maintained in the Employees table. This also includes
department ID numbers.
To compare and retrieve data from more than one table during a query, use the
WHERE clause to join two tables through common information.
For example, to return employee names, start dates, department names, and salaries
for employees that work for the HR department, you would build a query that looks
like this:
<CFQUERY NAME="GetEmployees" DATASOURCE="CompanyInfo">
SELECT Departments.Department.Name,
Employees.FirstName,
Employees.LastName,
Employees.StartDate,
Employees.Salary
FROM Departments, Employees
WHERE Departments.Department_ID = Employees.Department_ID
AND Departments.Department_Name = ’HR’
</CFQUERY>
•
Prefix each column in the SELECT statement to explicitly state which table the
data should be retrieved from.
•
The Department_ID field is the primary key of the Departments table and the
Foreign Key of the Employees table.
Building Flexible Search Interfaces
Frequently, you will want users to optionally enter multiple search criteria.
Wrap conditional logic around the SQL AND clause to build a flexible search interface.
To test for multiple conditions, wrap additional CFIF tags around additional AND
clauses.
For example, to allow users to search for employees by last name, department, or both,
you would build a query that looks like this:
<CFQUERY NAME="GetEmployees" DATASOURCE="CompanyInfo">
SELECT Departments.Department.Name,
Employees.FirstName,
Employees.LastName,
Employees.StartDate,
Employees.Salary
FROM Departments, Employees
WHERE 1=1
Summary of Contents for COLDFUSION 4.5-DEVELOPING WEB
Page 1: ...Allaire Corporation Developing Web Applications with ColdFusion ColdFusion 4 5...
Page 14: ...xiv Developing Web Applications with ColdFusion...
Page 26: ...xxvi Developing Web Applications with ColdFusion...
Page 34: ...8 Developing Web Applications with ColdFusion...
Page 70: ...44 Developing Web Applications with ColdFusion...
Page 84: ...58 Developing Web Applications with ColdFusion...
Page 114: ...88 Developing Web Applications with ColdFusion...
Page 148: ...122 Developing Web Applications with ColdFusion...
Page 174: ...148 Developing Web Applications with ColdFusion...
Page 208: ...182 Developing Web Applications with ColdFusion...
Page 244: ...218 Developing Web Applications with ColdFusion...
Page 274: ...248 Developing Web Applications with ColdFusion...
Page 288: ...262 Developing Web Applications with ColdFusion...
Page 300: ...274 Developing Web Applications with ColdFusion...
Page 350: ...324 Developing Web Applications with ColdFusion...
Page 362: ...336 Developing Web Applications with ColdFusion...