ColdFusion Tutorial

Finding Duplicates with SQL

Here's a handy query for finding duplicates in a table. Suppose you want to find all email addresses in a table that exist more than once:

<cfquery name="test" datasource="datasource" username="username" password="password">

SELECT email, COUNT(email) AS NumOccurrences

FROM users

GROUP BY email

HAVING ( COUNT(email) > 1 )

</cfquery>

You could also use this technique to find rows that occur exactly once:

<cfquery name="test" datasource="datasource" username="username" password="password">

SELECT email

FROM users

GROUP BY email

HAVING ( COUNT(email) = 1 )

</cfquery>

If you want to remove all duplicates in a table, try this:

<cfquery name="test" datasource="datasource" username="username" password="password">

Select distinct email FROM users

GROUP BY users

</cfquery>

SQL: DISTINCT Clause

The DISTINCT clause allows you to remove duplicates from the result set.  The DISTINCT clause can only be used with select statements.

ColdFusion Tutorial test

Finding Duplicates with SQL

No test is available or implemented for this ColdFusion Tutorial.

No User Comments.

No User Comments, be the first one to write your comments?

Add your comments

Your Name*:
Your Email:
Site URL:
Site Name:
Comemt Title*:
Your Comment*:
Key Phrase*:
4Dy
Your IP Address: 38.103.63.62
 

ScandicWeb is own by Scandic Systems LTD [UK] Company No. 5984000. All other trademarks and copyrights are the property of their respective holders.

[Home [Site Map [Privacy Statement [Site Feedback] [About ScandicWeb]


[ColdFusion [Flash [Search Engine Optimization [Graphics [Cascading Style Sheets (CSS) [JavaScript (JS) [Software]

Partner sites: ScandicSoft


Server time: 19. November 2008 Wednesday