0

I want to select rows that have a distinct email, see the example table below:

Table Name = Users

 +----+---------+-------------------+-------------+
   | id | title   | email             | commentname |
   +----+---------+-------------------+-------------+
   |  3 | test    | [email protected]   | rob         |
   |  4 | i agree | [email protected]   | rob         |
   |  5 | its ok  | [email protected]   | rob         |
   |  6 | hey     | [email protected]   | rob         |
   |  7 | nice!   | [email protected] | simon       |
   |  8 | yeah    | [email protected]  | john        |
   +----+---------+-------------------+-------------+

The desired result would be:

 +----+-------+-------------------+-------------+
   | id | title | email             | commentname |
   +----+-------+-------------------+-------------+
   |  5 | its ok| [email protected]   | rob         |
   |  7 | nice! | [email protected] | simon       |
   |  8 | yeah  | [email protected]  | john        |
   +----+-------+-------------------+-------------+

Distinct value should be latest entry in Table Example id = 6 What would be the required SQL?

2
  • Wouldn't latest row be id = 6? Commented Jan 18, 2019 at 10:39
  • my mistake it should be 6 Commented Jan 18, 2019 at 10:42

3 Answers 3

4

If you are using MySQL 5.7 or earlier, then you may join your table to a subquery which finds the most recent record for each email:

SELECT t1.id, t1.title, t1.email, t1.commentname
FROM yourTable t1
INNER JOIN
(
    SELECT email, MAX(id) AS latest_id
    FROM yourTable
    GROUP BY email
) t2
    ON t1.email = t2.email AND t1.id = t2.latest_id;

If you are using MySQL 8+, then just use ROW_NUMBER here:

WITH cte AS (
    SELECT id, title, email, commentname,
        ROW_NUMBER() OVER (PARTITION BY email ORDER BY id DESC) rn
    FROM yourTable
)

SELECT id, title, email, commentname
FROM cte
WHERE rn = 1;

Note: Your expected output probably has a problem, and the id = 6 record is the latest for [email protected].

Sign up to request clarification or add additional context in comments.

1 Comment

Nice ones :) I suggest you to sort by IDs on second query (tested and the results are sorted such as ids are 8, 6, 7)
2

You can try below using correlated subquery

select * from table1 a
where id in (select max(id) from table1 b where a.email=b.email group by b.email)

Comments

0

If 'id' is unique or primary key you could use this one:

select * from Users where id in (select max(id) from Users group by commentname)

Above one would up your database performance because the correlated subqueries comes from the fact that the subquery uses information from the outer query and the subquery executes once for every row in the outer query.So,I will suggest you using my answer if 'id' is unique.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.