-1

i use distinct to eliminate duplicate fields.

Query:

SELECT DISTINCT
  userId1, userId2, message, sentDate, ad1, avatar1, ad2, avatar2 
FROM 
(SELECT m.id, m.userId1, m.userId2, m.message, m.sentDate, m.readDate,
   u1.id id1, u1.ad ad1, u1.avatar avatar1, u2.id id2, u2.ad ad2, 
   u2.avatar avatar2 
 FROM (messages m inner join users u1 on m.userId1 = u1.id) 
 inner join users u2 on m.userId2 = u2.id
 where (userId1 = 8 or userId2 = 8) 
 order by sentDate desc
) as q

response

if one field equal to userId1 = 7 and userId2 = 8 don't take userId1 = 8 and userId2 = 7. How can i say this to sql?

The Impaler
  • 45,731
  • 9
  • 39
  • 76
Kaan
  • 5
  • 6

1 Answers1

0

Use one of the techniques in SQL select only rows with max value on a column to get the row with the highest sentDate in a group. Use

GROUP BY GREATEST(userid1, userid2), LEAST(userid1, userid2)

as the grouping.

Barmar
  • 741,623
  • 53
  • 500
  • 612