检查表中的多行,如果行匹配,则将它们计为1

时间:2016-04-18 17:46:29

标签: php mysql

我有一个名为private_messages的表格,其结构如下:

id     message_from     message_to

我正在尝试确定活动聊天室的数量,但该表存储了所有已发送和已接收的消息,因此Alice位于message_frommessage_to的行可能很多}。

假设我的private_messages表有4行:

id   message_from    message_to
1    Alice           conor
2    Alice           conor
3    connor          Alice
4    Anderson        conor

Alice和conor之间的对话应被视为1。因此,当我回音时,让我们说$active_conversations,应该打印数字2

我假设这涉及一个阵列?即$conversations = [];。但我不知道或不了解该怎么做。

3 个答案:

答案 0 :(得分:0)

select count(id) from private_messages group by message_from,message_to

然后你得到适当的结果

答案 1 :(得分:0)

如果你只是想得到一些对话,你可以使用这样的东西

SELECT COUNT(DISTINCT
             CONCAT(
                    LEAST(message_from,message_to),
                    '->',
                    GREATEST(message_from,message_to)
                    )
             ) AS active_conversations
FROM private_messages;

sqlfiddle

输出:

active_conversations
2

如果您想获得对话的详细信息,可以使用此

SELECT DISTINCT
       LEAST(message_from,message_to) as FirstPerson,
       GREATEST(message_from,message_to) as SecondPerson
FROM private_messages;

sqlfiddle

输出:

FirstPerson     SecondPerson
Alice           conor
Anderson        conor

答案 2 :(得分:0)

其中一个问题是`private_messages`中的元组('connor','Alice')和('Alice','connor')代表了你正在计算的同一个会话/房间。

一种方法是获得不同值的总计数,除以2,但这似乎不是正确的方法。 (我们可以设置一些边缘情况,我们得到的结果不是整数,或者小于活动房间的实际数量......例如

id   message_from    message_to
--   ------------    ----------
1    Alice           conor
2    Alice           conor
3    conor           Alice
4    Anderson        conor
5    Cooper          Alice
6    Dalton          Allman

另一种方法是重新排序元组中的值(交换message_from和message_to),它们与会话另一侧的message_from和message_two匹配。例如,我们可以比较message_from和message_to,如果message_to比message_from“更低”则交换它们。

作为示例,使用表达式返回m_one和m_two列中显示的值。

id   message_from    message_to      m_one       t_two
--   ------------    ----------      -----       -----
1    Alice           conor           Alice       conor
2    Alice           conor           Alice       conor
3    conor           Alice           Alice       conor
4    Anderson        conor           Anderson    conor
5    Cooper          Alice           Alice       Cooper
6    Allman          Dalton          Allman      Dalton

然后我们可以计算(m_one,m_two)元组的不同值。

第三种方法是仅丢弃message_from值高于message_to的任何元组,摆脱会话/房间的一侧。然后计算剩下的不同元组。但同样,这可能会排除一些仅在一个方向上发送消息的会话/会议室。

id   message_from    message_to     m_one       t_two
--   ------------    ----------     -----       -----
1    Alice           conor          Alice       conor
2    Alice           conor          Alice       conor
3    conor           Alice          Alice       conor
4    Anderson        conor          Anderson    conor
5    Cooper          Alice          (NULL)      (NULL)
6    Allman          Dalton         Allman      Dalton

(我们将讨论对话/房间的角落情况,其中message_from等于message_to。)

我们编写的SQL实际上取决于我们想要实现的算法。

我们可以使用GROUP BY来折叠“匹配”行,并获得计数。或者我们可以使用COUNT(DISTINCT foo)来计算不同的值。

如果我们选择需要“交换”值的算法,我们可以使用使用LEASTGREATEST函数的表达式。或MySQL IF()函数或CASE表达式。

如果我们想要排除计数行,我们可以在WHERE子句中包含谓词。

所有这些都是可能的,并且有多种方法可以实现指定的结果。