Sajeev C Sajeev C - 3 months ago 8
MySQL Question

mysql- fetch values for unique email ids

The database table where I have to run this query has redundant email ids stored. Table stores

emailid
and
clientid
. But
clientid
is always unique as it's a primary key auto incremented field

I want to fetch all
clientid
for unique email id.

Let's say info@info.com is used for 2 different users. I want to exclude one of them. Let's say FCFS. I wanna include first email and exclude the next one.

This is the query that I wrote but it doesn't give me results with unique email id.

SELECT DISTINCT (email), clientid
FROM clients
WHERE status=1

Answer

What you need it's the group by statement

 SELECT email, clientid 
    FROM clients 
    WHERE  status=1  
    GROUP BY email