【问题标题】:Count unread messages from a sender计算来自发件人的未读消息
【发布时间】:2014-07-14 22:27:27
【问题描述】:

我正在使用 mysql 和 php 构建一个消息传递系统。我已经到了要选择用户收到的消息并计算从不同用户收到的未读消息到同一用户的地步。我已经在下面说明了它

table----users
perID | name |
 001  | mum  |
 002  | tok  |
 003  | sat  |

table----messages
msgID |senderID | msgBody |      msgTime       | deleted_by_sender |
 200  | 002     | Hello   | 2014-07-13 19:14:22|  no               |
 201  | 002     | Mate    | 2014-07-13 19:14:29|  no               |
 202  | 003     | hi mum  | 2014-07-13 19:19:12|  no               |
 203  | 003     | How     | 2014-07-13 19:19:52|  no               |

来自父表 userssenderID 引用

   table----recipients
 recID |msgID |recipientID | msgStatus| deleted_by_recipient|       
 310   | 200  |    001     |  unread  |        no           |
 311   | 201  |    001     |  unread  |        no           |
 312   | 202  |    001     |  read    |        no           |
 313   | 203  |    001     |  read    |        no           |

recipientID 引用父表users

我想要

1. Get only the current message received by the recipient with recipientID=001
    if it is not deleted by the recipient.

2. count the number of unread messages received from the individual users.

如下所示

senderID | msgID | unread |
   002   |  201  |  2     |
   003   |  203  |  0     |

我下面的查询按预期工作,但它隐藏了最后一行,因为它在 msgStatus 列中没有未读值, 但我希望即使msgStatus 没有任何价值,也会返回所有行。它也应该在一个优化的查询中。

SELECT *,count(msgStatus) As unread  
FROM (
        SELECT
                m.senderID,
                m.msgTime,
                u.perID,
                r.recipientID,
                r.msgID,
                r.msgStatus,
                r.deleted_by_recipient
        FROM 
                messages m
        INNER JOIN 
                users u
            ON 
                m.senderID=u.perID
        INNER JOIN
                recipients r
            ON
                r.msgID=m.msgID

        ORDER BY msgTime DESC
    )h 
WHERE 
    recipientID=12 and 
    deleted_by_recipient ='no' and 
    msgStatus='unread'
GROUP BY perID

感谢您的帮助。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    您可以使用条件聚合做您想做的事。想法是将条件从where 子句移到select 子句:

    select senderid,
           max(case when r.deleted_by_recipient = 'no' and r.recipientID = '001' then m.msgID end
              ) as CurrentMsg,
           sum(r.msgStatus = 'unread') as unread
    from messages m left outer join
         recipients r
         on  m.msgID = r.msgID
    group by senderid;
    

    我不能 100% 确定这实现了您的逻辑:

    1. 这假定最近的消息是具有最大MsgID 的消息。基于另一个字段是可能的,最容易通过 substring_index()/group_concat() 技巧完成。
    2. 这是对所有未读邮件的计数,与收件人无关。再一次,这很容易通过更改 sum() 中的逻辑来解决。
    3. 您的示例数据没有重复项(相同的MsgId 有多个收件人)。如果可能,您可能需要更改计数的逻辑。再说一遍,这并不难,只是不清楚是否需要额外的工作。

    【讨论】:

    • @George 这里是一个 SQL FIDDLE,答案显示它可以工作.. 它需要一个简单的编辑,但工作起来就像一个魅力:) ------ sqlfiddle.com/#!2/6529d/1
    • 这仍然会计算 msgStatus 中的所有未读消息,而不管收件人是谁。有没有办法解决这个问题?
    • 请问我上面的查询不能修改以适合我的情况吗?
    • @乔治。 . .即点号 (2)。您可以通过添加 where 子句将所有结果限制为一个收件人来解决此问题。
    猜你喜欢
    • 1970-01-01
    • 2014-12-29
    • 2021-09-09
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2020-07-28
    相关资源
    最近更新 更多