Mysql statement to select distinct rows from two columns

痞子三分冷 提交于 2019-12-06 15:13:24

Just use the expressions in the second query for the GROUP BY of the first query:

SELECT * FROM chat_messages GROUP BY LEAST(to_id,from_id), GREATEST(to_id,from_id)

If second query returns what you expect, then below query returns rest information:

SELECT *
FROM chat_messages t1 INNER JOIN (
    SELECT DISTINCT LEAST(to_id, from_id) AS value1,
        GREATEST(to_id, from_id) AS value2
    FROM chat_messages
) t2 ON t1.to_id = value1 AND t1.from_id = value2;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!