Guest User

Untitled

a guest
Jan 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. id | from | to
  2. ------------------
  3. 1 | John | Robert
  4. 2 | John | Michael
  5. 3 | Robert | John
  6. 4 | Michael | John
  7.  
  8. John | Robert
  9. John | Michael
  10.  
  11. SELECT `from`,`to` FROM s_msgs WHERE id IN(
  12.  
  13. SELECT id FROM (
  14. SELECT MIN(id) AS id,
  15. CASE
  16. WHEN STRCMP(`to`,`from`) = -1 THEN CONCAT(`to`,`from`)
  17. ELSE CONCAT(`from`,`to`)
  18. END
  19. AS conc
  20. FROM s_msgs
  21.  
  22. GROUP BY conc
  23.  
  24. ) AS t
  25. )
  26.  
  27. SELECT DISTINCT `from`, `to`
  28. FROM YOUR_TABLE
  29. UNION
  30. SELECT DISTINCT `to`,`from`
  31. FROm YOUR_TABLE
  32.  
  33. SELECT *
  34. FROM MY_TABLE f
  35. WHERE NOT EXISTS (
  36. SELECT 1
  37. FROM MY_TABLE t
  38. WHERE f.`from` = t.`to`
  39. AND f.`to` = t.`from`
  40. AND f.id > t.id)
Add Comment
Please, Sign In to add comment