0

I am using MYSQL database.
I have a table with four columns id, sender, reciever and recieverread; reciever and sender columns can contain duplicate values is there a way to select only the last row with the duplicate value?

I tried
SELECT DISTINCT sender FROM msg WHERE sender = sender

3
  • 1
    Provide SHOW CREATE TABLE for your table, and some sample data Commented Feb 20, 2014 at 12:00
  • Last based on what criteria? Commented Feb 20, 2014 at 12:00
  • Your example is just selecting the sender? How do you know which row it is coming from? Commented Feb 20, 2014 at 12:06

5 Answers 5

1

You can try something like this:

SELECT sender FROM msg GROUP BY sender HAVING COUNT(sender) > 1;
Sign up to request clarification or add additional context in comments.

Comments

0

This will prevent to data duplication. You may try this,

SELECT DISTINCT sender FROM msg WHERE sender = sender group by sender

Comments

0

How about:

SELECT MAX(id) AS id_last, sender, receiver, recieverread FROM msg GROUP BY sender, receiver, recieverread

Comments

0

try this

select maxid ,sender from msg where id in (
                       select max(id) as maxid from msg GROUP BY sender)

Comments

0

A standard way to approach this in MySQL is with a not exists clause:

select m.*
from msg m
where not exists (select 1 from msg m2 where m2.sender = m.sender and m2.id > m.id);

That is, select the row for a given sender that has no larger ids in the table.

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.