SELECT Field1, Field2, …FieldN
FROM TableName;
|
Clause |
Required/Optional |
Purpose |
Example |
|
SELECT |
Required |
Indicates the field(s) to be retrieved. |
SELECT Name, Email |
|
FROM |
Required |
Indicates the table(s) to retrieve the fields from |
FROM tblEmployee |
|
WHERE |
Optional |
Indicates a filter criteria |
WHERE Email NOT NULL |
|
ORDER BY |
Optional |
Indicates the sort fields and sort order (ascending is the default and descending is indicated by DESC) |
ORDER BY Name; or ORDER BY Name DESC; |
i.
Command: SELECT
ii.
Format: SELECT Field1, Field2, …FieldN
FROM TableName
WHERE Criteria
ORDER BY
Sortfield1, Sortfield2,…SortfieldN
iii.
Example: SELECT FName, LName, Email
FROM tblEmployee
WHERE LName = ‘Doe’
ORDER BY
Name;
1. SQL statements are not case sensitive. However, it is common practice to put SQL keywords in all capitals, as shown above.
2. All white space is ignored in a SQL statement. However, SQL statements are much easier to read when broken up over several lines. Again, this is not required but it is a common practice among programmers.
3. You can retrieve data from multiple tables by joining tables, specifying more than one table in the FROM clause, and scoping your field names with the appropriate table name. For example,
SELECT tblEmployee.Name, tblRetire.RetireDate (scoped fields)
FROM tblEmployee, tblRetire (multiple tables)
WHERE tblEmployee.SSNo = tblRetire.SSNo; (table join)
4. Perhaps the most important rule of all…the ORDER BY clause must ALWAYS be the last clause in the SQL statement, otherwise the clause is ignored entirely.
<CFQUERY DATASOURCE=”Employee” NAME=”qGetEmp”>
SELECT Name, Email
FROM tblEmployee
ORDER BY Name;
</CFQUERY>
i. Time Savers!
1. Naming conventions- Make ‘em and Stick to ‘em! Notice how I named the query. q indicates that it is a query and GetEmp indicates that I am retrieving employee information. Again, you are in no way required to name you queries in this manner, but establishing (and sticking to) naming conventions will save you a lot of time in the long run. This convention is common among CF programmers.
2. SELECT all – You can use a * (asterisks) wildcard in your SELECT clause to indicate you want all fields returned from your database. However, use this sparingly because returning all records takes more processing time. Here’s an example of the code.
SELECT *
FROM tblEmployee
ORDER BY Name;
ii. Start a blank template named GetEmp.cfm and title it Get Employee Information.
iii. Write the code to retrieve all records from the employee database and sort the records by last name.
iv. How can you verify that your code works properly? Try printing out the values of your newly retrieved variables. Does it work?
i. We need <CFOUTPUT> tags to indicate we want to display the value of a variable and
ii. We need # signs to differentiate between variable names and regular text
i. Query variables must be scoped with the query name (hint: that’s why it’s good to keep query names short and sweet).
<CFOUTPUT>
<UL>#qGetEmp.Name# #qGetEmp.Email#</UL>
</CFOUTPUT>
The above code works perfectly…if all you want to do is output the values of the first record. But, what if you want to output the values of ALL the records retrieved? Imagine that! Somehow we need to tell CF to “loop” through the commands within the <CFOUTPUT> tags until all records have been printed.
ii. We do this with the following modification to our <CFOUTPUT> tag.
<CFOUTPUT QUERY=”qGetEmp”>
<UL>#qGetEmp.Name# #qGetEmp.Email#</UL>
</CFOUTPUT>
i. Modify your GetEmp.cfm template to display the FName, LName and Email fields in a bulleted list and sorted by Last Name. Add an <h3> title to the list; Bulleted Email Address List.
ii. Just below that code, try to re-display your records again in the following format. Give this table the title <h3>“Email Address Table.” </h3>
|
Name |
Email |
|
Jane Doe |
jdoe@where.com |
|
John Doe |
jdoe@here.com |
|
Jeannie Doe |
jdoe@there.com |
Hmm….Do you have to re-query the database? Will your table tags go inside the <CFOUTPUT> tags or outside? How about header tags, inside or outside <CFOUTPUT>? And <TR> tags?