我有一个名为email的表,该表具有三列,分别是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 |
--------------------------------------------------

现在我的问题是,如何找出两个客户之间的最高对话

即A向B发送邮件,而B向A发送邮件,则表示他们的 session 数为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

请参阅 fiddle here

09-25 18:41