Creating Table Joins; Building Flexible Search Interfaces - MACROMEDIA COLDFUSION 4.5-DEVELOPING WEB Develop Manual

Developing web applications with coldfusion
Table of Contents

Advertisement

40

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
Employees.FirstName,
Employees.LastName,
Employees.StartDate,
Employees.Salary
FROM Departments, Employees
WHERE 1=1
Departments.Department.Name,
Developing Web Applications with ColdFusion

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the COLDFUSION 4.5-DEVELOPING WEB and is the answer not in the manual?

Subscribe to Our Youtube Channel

This manual is also suitable for:

Coldfusion 4.5

Table of Contents