我有表“s_msgs”,他的结构是
id | from | to
------------------
1 | John | Robert
2 | John | Michael
3 | Robert | John
4 | Michael | John
我需要获取每对消息的发送方-接收方,即结果必须是
John | Robert
John | Michael
我写了查询,但我认为这不是最佳变体,更希望在表中会有几百万行,所以谁能告诉更多的最佳查询?
这是我的查询
SELECT `from`,`to` FROM s_msgs WHERE id IN(
SELECT id FROM (
SELECT MIN(id) AS id,
CASE
WHEN STRCMP(`to`,`from`) = -1 THEN CONCAT(`to`,`from`)
ELSE CONCAT(`from`,`to`)
END
AS conc
FROM s_msgs
GROUP BY conc
) AS t
)