duandang9434 2016-07-13 09:22
浏览 218
已采纳

Php mysql未读用户的消息

I am using a message system inside my application and i want to get the number of unread messages in his profile the schema is as follows

Users table     message

user_id         message_id 
......          sender_id
                receiver id
                text 
                flag

flag where is use to determine whether a user has read a message or not . flag=0 user has not read a message and 1 as read

how to get the number of unread message of a particular user

SELECT message_id, count(*)
FROM message
WHERE flag=0
GROUP BY receiver_id;
  • 写回答

2条回答 默认 最新

  • doucong7963 2016-07-13 09:25
    关注

    You're almost there, try this:

    SELECT receiver_id, count(*) as unread_cnt
    FROM message
    WHERE flag = 0
    AND receiver_id = '$particular_user_id'  // If you want all users, you can comment out this line.
    GROUP BY receiver_id;
    

    Also you can get read count by following:

    SELECT
        receiver_id,
        COUNT(CASE WHEN flag = 0 THEN receiver_id END) as unread_cnt,
        COUNT(CASE WHEN flag = 1 THEN receiver_id END) as read_cnt
    FROM message
    WHERE receiver_id = '$particular_user_id' // If you want all users, you can comment out this line.
    GROUP BY receiver_id;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题