![]()
Problem
Write a tool that allows you to email all users. No one should get more than one email even if they are on the list more than twice.
The Catch
The twist to this problem is that the table of users is set up with a primary and a secondary email field and the same email can appear in multiple records in either the primary or secondary field.
Example Table
| UserID | Name | Email1 | Email2 |
| 1 | Fred Smith | tim.smith@domain.com | mary.smith@domain.com |
| 2 | Sarah Smith | tim.smith@domain.com | mary.smith@domain.com |
| 3 | Sandy Jones | mark.jones@domain.com | linda.jones@domain.com |
| 4 | Tom Johnson | laurie.johnson@domain.com | tim.smith@domain.com |
In the above example, We have Tim twice in Email1 and once in Email2. If we did a SQL Group by we would still have Tim twice. Once in Email1 and Once in Email2. It wouldn’t take long before Tim is calling asking us to fix this annoying problem of getting two emails.
The Solution
ColdFusion Query of Query and the good ol’ UNION feature in SQL!
<cfquery name="qryUsers" datasource="#request.dsn#"> Select Email1 as Email From Users Group by Email1 UNION SELECT Email2 AS Email FROM Users Group by Email2 </cfquery> <cfquery name="qryEmails" dbtype="query"> Select Email From qryUsers Group by Email </cfquery>
The above example does two queries. The first one qryUsers uses UNION to combine two queries of the same table both retrieving email fields. With UNION you need to make sure that you name the fields the same from both queries and that they are both of the same data type. The second query qryEmails is a simple ColdFusion Query of Query which then takes the resulting recordset and groups once again to get rid of the second instance of Tim. This can also work with two different tables in the event you have a user table and maybe a Admin table but you want to send one email to everyone.
Now, isn’t programming fun?
There are other ways to solve this common problem. I’d love to hear yours!
Vince Collins