我有一个名为电子邮件的表,其中三列表示id,emailFrom,emailTo
-------------------------------------------------- | id | emailFrom | EmailT0 | -------------------------------------------------- | 1 | A | B | -------------------------------------------------- | 2 | B | A | -------------------------------------------------- | 3 | A | B | -------------------------------------------------- | 4 | C | A | -------------------------------------------------- | 5 | B | C | -------------------------------------------------- | 6 | A | C | -------------------------------------------------- | 7 | A | B | --------------------------------------------------
现在我的问题是,如何找出两个客户之间的最高对话
即发送邮件给B和B发送邮件给A,这意味着他们有计数2的对话.
现在,
我想找到哪些用户相互发送了大多数电子邮件
解决方法:
这将返回已发送大多数电子邮件的用户:
SELECT
LEAST(emailFrom, emailTo) email1,
GREATEST(emailFrom, emailTo) email2,
COUNT(*)
FROM
yourtable
GROUP BY
LEAST(emailFrom, emailTo),
GREATEST(emailFrom, emailTo)
ORDER BY
COUNT(*) DESC
LIMIT 1
请参阅小提琴here.