To help Compass Travel agents take trip reservations by telephone and in person, the trip coordinator maintains a list of current trip offerings. Years ago, the coordinator would type the list and fax it to the various Compass Travel offices in an effort to keep everyone informed. When Compass Travel built an intranet accessible by all offices, the trip coordinator added the following HTML web page to the site:
Each time the Trip List HTML page is rendered in a browser, it displays the same web page. Since the page always shows an identical trip list, it is considered a static web page. You should only use static web pages when you are creating a page that is not likely to change often.
Using the static web page approach, the Trip Coordinator needs to modify all the web pages that reference trip lists when trips are added, deleted, or trip names are changed. This manual process of updating each web page can lead to inaccurate or untimely information. Luckily, since Compass Travel has built a database that contains a list of trips, you can build a more accurate and timely solution for the trip coordinator. To accomplish this, you must understand how to issue a SQL SELECT statement to retrieve the data from the Trips table in the Compass Travel database.
The SQL SELECT statement retrieves columns of data from a database. The tabular result is stored in a result table (called the record set).
You use the following SELECT statement to retrieve information from a table:
SELECT column_name(s) FROM table_name
Consider a table named Clients to hold information about people with the following rows:
LastName |
FirstName |
Address |
City |
---|---|---|---|
Jones |
Tom |
12 State St |
Boston |
Adams |
Anita |
521 Beacon St |
Boston |
Green |
Peter |
1 Broadway |
New York |
To select the columns named LastName and FirstName, use the following SELECT statement:
SELECT LastName, FirstName FROM Clients
The results of this SQL statement contains the following data:
LastName |
FirstName |
---|---|
Jones |
Tom |
Adams |
Anita |
Green |
Peter |
To conditionally select data from a table, you can add a WHERE clause to the SELECT statement resulting in the following syntax:
SELECT column_name FROM table_name WHERE column condition value
With the WHERE clause, you can use any of the following operators:
For example, to select the columns named Last Name and First Name for Clients whose City is Boston, use the following SELECT statement:
SELECT LastName, FirstName FROM Clients Where City = 'Boston'
The results of the preceding SQL statement contains the following data:
LastName |
FirstName |
---|---|
Jones |
Tom |
Adams |
Anita |
You can compose a WHERE clause with one or more conditions; these are called subclauses. You join subclauses using the operators AND and OR.The AND operator displays a row if ALL conditions listed are true. The OR operator displays a row if ANY of the conditions listed are true. An example of a WHERE clause with multiple subclauses follows:
SELECT LastName FROM Clients Where City = 'Boston' AND FirstName = 'Anita'
The results of the preceding SQL statement contains the following data:
LastName |
---|
Adams |
Note: The preceding SQL SELECT examples use single quotation marks around the value. SQL uses single quotation marks around text values. Most database systems will also accept double quotation marks. Do not enclose numeric values in quotation marks.
You use the ORDER BY clause to sort the result rows. The following SQL statement returns an alphabetic list of people sorted by last name then first name from the Clients table:
SELECT * FROM Clients Order By LastName, FirstName
The default is to return the results in ascending order (top to bottom). If you include the DESC keyword in the ORDER BY clause, the rows are returned in descending order (bottom to top).
The following statement returns a reverse alphabetic list of the Clients table:
SELECT * FROM Clients Order By LastName, FirstName DESC
Note: The SQL SELECT statement is quite powerful. There are several other options for retrieving data from a SQL database using the SELECT statement, which are not described in this book. For more information, consult a SQL reference.
Relational database management systems process SQL instructions sent to them from various applications. ColdFusion sends SQL statements to database managers to manipulate data. ColdFusion needs a way to know which database manager to send a specific SQL string for evaluation. In CFML, the cfquery
tag serves this purpose. You will use the SQL SELECT statement and the cfquery
tag to create a dynamic version of the Trip List page presented earlier in this lesson. In this example, you use cfquery
to return all the trip names found in the tripName column within the Compass Travel Trips table. To use the SQL SELECT statement to dynamically retrieve this information, you must execute the SQL SELECT statement between the cfquery
start and end tags as follows:
<cfquery name="TripResult" datasource= "CompassTravel">
SELECT tripName FROM trips </cfquery>
In Chapter 2, you learned that the ColdFusion cfoutput
tag is an easy mechanism to display literal text and the contents of variables. Additionally, the cfoutput
tag significantly simplifies displaying the results of queries. When used to display the results from a query, the cfoutput
tag automatically loops through the record set for you. You simply specify the name of the query in the QUERY attribute of the cfoutput
tag:
<cfoutput query="TripResult">
All the code between the cfoutput
start and end tags is the output code block. The output code block executes repeatedly, once for each row in the record set. However, if the query returns no rows, ColdFusion skips the code contained in the output code block.
<cfoutput query = "xxx">
...output code block... </cfoutput>
In CFML you surround variables with pound signs (#) to display their contents using the cfoutput
tag. You also use this approach with column names specified in the SELECT statement of a cfquery
. For instance, when you want to display the trip names from the SQL query, you would simply use #tripName#
within the output code block.
<cfoutput query="TripResult">
#tripname#
</cfoutput>
For additional information about using SQL with cfquery
and cfoutput
, see Developing ColdFusion MX Applications with CFML.
In the following exercises you will build a dynamic Trip Listing web page that is always current. The first exercise guides you through constructing a query to retrieve information from the database. In the second exercise, you will enhance the query to sort the query results and to display other pertinent trip information.
For your convenience, the following figure shows the Compass Travel Trips table. You can refer to this table to verify the names of the columns you use in the queries in the exercises.
Follow these steps to build a query that lists the current trips from the Compass Travel database.
<cfquery name="TripResult" datasource="compasstravel"> SELECT tripName FROM trips </cfquery> <html> <head> <title>Trip Listing</TITLE> </head> <body> <h1>Trip List</h1> <cfoutput query="TripResult">#tripName#<BR></cfoutput> </body> </html>
The following table describes the code used to build the query:
In this exercise you will improve the Trip List page to make it easier for the Compass Travel agents to locate trips. You must make the following improvements:
To enhance the trip listing query to meet these new requirements, you will modify the query you created in the previous exercise.
Follow these steps to enhance the query to meet the new requirements. Display the triplisting.cfm page in the browser after each step to ensure the corresponding requirement was met.
cfquery
tags as follows:SELECT tripName FROM trips ORDER BY tripName
SELECT tripName, departureDate, returnDate, price FROM trips ORDER BY tripName
</cfoutput>
tag) from just #tripName#
to include all three selected fields, as follows:#tripName# departs: #departureDate# returns: #returnDate# price:
#price#<BR>
SELECT tripName, departureDate, returnDate, price
FROM trips
WHERE price <= 1500
ORDER BY tripName
<h1>Trip List</h1>
to <h1>Budget Trip List</h1>
.
Note that the dates and prices in the preceding listing are not formatted. In Lesson 3 you will enhance the look of this page.