There is such a request:

SELECT * FROM (SELECT * FROM mess WHERE who='2' or whom='2' ORDER BY id DESC) as tmp GROUP BY who, whom ORDER BY id DESC 

It makes a selection from the end of the table and as a result it turns out something like this:

 id who whom 13 2 1 11 1 2 9 3 2 7 2 3 4 2 5 

How now to make it so that only records remain without repetitions in who and whom, and with a large id. That is, in the end it should be like this:

 id who whom 13 2 1 9 3 2 4 2 5 

Maybe a little clumsy explained, if something is not clear, specify.

    2 answers 2

     SELECT DISTINCT * FROM mess WHERE (who='2' or whom='2') and id > чего то (или max(id) ) ORDER BY id DESC 
    • And what does id> mean something (what's this? Where to get this value? Or max (id)?) It hasn't helped yet. Everything chooses. - Svyatoslav
    • Well, what do you want to go? everything? leave only max (id), if more than some, then id> 10, for example - Gorets
    • No, the point is that if there is a who = 2 and whom = 1 entry, then who = 1 and whom = 2 is no longer necessary. - Svyatoslav
    • SELECT DISTINCT for this purpose - Gorets
    • Although I don’t know more than what value should be id, and max (id) does not work inside WHERE, I tried to make such a query. I turned out more and more than 100. No uniqueness. SELECT DISTINCT * FROM mess WHERE (who = '2' or whom = '2') and id> 100 ORDER BY id DESC - Svyatoslav

    All found the answer.

     SELECT * FROM (SELECT * FROM mess WHERE who='2' or whom='2' ORDER BY id DESC) as tmp GROUP BY LEAST( who, whom ) , GREATEST( who, whom ) ORDER BY id DESC