Home > Database > Mysql Tutorial > How Can I Retrieve Only Unique Email Addresses from a Multi-Column SQL Query?

How Can I Retrieve Only Unique Email Addresses from a Multi-Column SQL Query?

Barbara Streisand
Release: 2024-12-28 11:37:20
Original
799 people have browsed it

How Can I Retrieve Only Unique Email Addresses from a Multi-Column SQL Query?

DISTINCT for a Specific Column

Given a query that retrieves multiple columns, such as ID, Email, ProductName, and ProductModel from the Products table, how can it be modified to display unique Email values only?

To address this, SQL Server 2005 and later versions offer a solution:

This query uses the ROW_NUMBER() function to assign a row number to each row within each Email partition, ordered in descending order of ID. The rn column then identifies which row should be included in the final result, as only those with rn equal to 1 will be selected.

To further refine the query, a WHERE clause can be added to filter the rows based on specific criteria. For instance, to include only rows with a specific ProductModel and whose ProductName contains a certain substring:

The above is the detailed content of How Can I Retrieve Only Unique Email Addresses from a Multi-Column SQL Query?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template