我有一个名为电子邮件的表格,其中有三列表示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次会话。
现在,
我想找到哪些用户相互发送了大多数电子邮件
答案 0 :(得分:5)
这将返回已发送大多数电子邮件的用户:
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。
答案 1 :(得分:0)
带有GROUP BY的直接COUNT(*)将执行此操作:
select emailFrom, emailTo, count(*) as conversation_count
from email
group by emailFrom, emailTo;