简体   繁体   中英

Select Unique Rows Based on Single Distinct Column - MySQL

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

Table Name = Users

 +----+---------+-------------------+-------------+
   | id | title   | email             | commentname |
   +----+---------+-------------------+-------------+
   |  3 | test    | rob@hotmail.com   | rob         |
   |  4 | i agree | rob@hotmail.com   | rob         |
   |  5 | its ok  | rob@hotmail.com   | rob         |
   |  6 | hey     | rob@hotmail.com   | rob         |
   |  7 | nice!   | simon@hotmail.com | simon       |
   |  8 | yeah    | john@hotmail.com  | john        |
   +----+---------+-------------------+-------------+

The desired result would be:

 +----+-------+-------------------+-------------+
   | id | title | email             | commentname |
   +----+-------+-------------------+-------------+
   |  5 | its ok| rob@hotmail.com   | rob         |
   |  7 | nice! | simon@hotmail.com | simon       |
   |  8 | yeah  | john@hotmail.com  | john        |
   +----+-------+-------------------+-------------+

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

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 rob@hotmail.com .

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM