Using SQL's DISTINCT to Create Dynamic Index Links

To add index links dynamically, we can use the DISTINCT command in our template's SQL statement. Recall from Listing 5.20 in Day 5 that DISTINCT tells ColdFusion to get only one item of each type. For example, this query

SELECT DISTINCT condition FROM 8tracks

returns the following from our table:

poor

passable

excellent

There are just three items, even though there are 14 records in the table. In effect, we're using DISTINCT to list the various classifications of the field condition.

If you use a DISTINCT query in conjunction with a grouped page, you can dynamically create links to groups. I've built a template for this in Listing 10.5.

Listing 10.5 Dynamically Creating Links to Groups
 1: ...

Get Sams Teach Yourself ColdFusion® in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.